当前位置:   article > 正文

python图书馆预约_利用Python爬虫抢座图书馆!

图书馆抓包seatid

1202e5e3-dabd-4d35-b212-93a3e8c8a262

4096d384ba914e6fa1b9d01a77c2b022

login.PNG

下面给大家看一下,fiddler抓包的结果(提交的form信息和请求的头部信息):

71b0c0bc229e42caa47504249a3847ff

form.PNG

请求头:

0b375ba36e5a4e6fada6b3885da0c569

捕获2.PNG

接下来小编给大家解释下form中各个参数的含义:

1 TxtBox1 &TxtBox2 后面的内容是固定不变的(我也不知道后面的值是什么鬼)

2 ddDay 后面跟的参数是你想要预约的日期(“今日”或“明日”)

3 ddRoom 一共有7位数字(第一位表示校区,即:1----“东区”,“2”----“中区”,3----“西区”;第2-4位表示 预约教室的门牌号;后三位“001”是固定的)所以上图中“3207001”指代的就是西区图书馆207自习室;

4 txtSeats:是已预约的座位/空闲座位

小编在撸代码的时候,以为这个网站没有反爬措施,是我天真了,竟然还有反爬!所以,还是老规矩需要在请求的头部信息加上headers,伪装成浏览器才行。

import requests

import http.cookiejar

from bs4 import BeautifulSoup

import pytesseract

import time

cookies = {} #构建一个空字典用来存放cookies

headers={

'Connection': 'Keep-Alive',

'Accept-Language': 'zh-CN,zh;q=0.8',

'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0',

'Accept-Encoding': 'gzip, deflate, sdch',

'Upgrade-Insecure-Requests':'1',

'Referer': 'http://172.16.47.84/',

'X-Requested-With': 'XMLHttpRequest',

'Host': '172.16.47.84',

}

def login(username, password, code):

url = 'http://172.16.47.84/'

form = {

'__VIEWSTATE': '/wEPDwUKMTM4MDI3NzI1MGRkScbX95yvCTaTc5BiKFlgoJSN0gi9TvPi4as2Sed98Ug=',

'__EVENTVALIDATION': '/wEWBQLYjc7zAwLs0bLrBgLs0fbZDALs0Yq1BQKM54rGBuOROfeko3/nasulQt/v8ihgd8TDoZ4pgdA4rtbVvanV',

'TextBox1': username,

'TextBox2': password,

'TextBox3': code,

'Button1': '登录',

}

resp = requests.post(url, headers=headers, data=form, cookies=cookies)

对了,这个网站也有验证码,而且还极为复杂,表示给用户带来了很不好的体验,用pytesseract识别的时候,成功率几乎为0.在下感觉这个开发者脑子里有坑,作为一个人类的我,倾斜过后的0和o,还有g和数字9 都不分清楚,更不要讲机器了。下面是验证码的处理:

def get_code():

url = 'http://172.16.47.84/VerifyCode.aspx?'

resp = requests.get(url, headers=headers)

cookies['ASP.NET_SessionId'] = resp.cookies.get('ASP.NET_SessionId')

with open('code.jpg', 'wb') as img:

img.write(resp.content)

def check_show():

checkcode = Image.open("code.jpg")

image = Image.open('code.jpg')

imgry = image.convert('L')

imgry.show()

成功登陆后就是选座了:

def cchose_seat():

try:

churl = "http://172.16.47.84/DayNavigation.aspx"

churl_1 = "http://172.16.47.84/AppSTod.aspx?roomid=3207&hei=834&wd=1920"

responce = requests.get(url)

form_1 = {

'__VIEWSTATE':'/wEPDwUJMTAxMjgzNTc0D2QWAgIDD2QWBgIFDxBkZBYBZmQCBw8QDxYGHg5EYXRhVmFsdWVGaWVsZAUGc2VhdGlkHg1EYXRhVGV4dEZpZWxkBQhsb2NhdGlvbh4LXyFEYXRhQm91bmRnZBAVCB7kuJzljLrlm77kuabppoboh6rkuaDlrqQ0MDHlrqQe5Lit5Yy65Zu+5Lmm6aaG6Ieq5Lmg5a6kMTAx5a6kHuS4reWMuuWbvuS5pummhuiHquS5oOWupDIwMeWupB7kuK3ljLrlm77kuabppoboh6rkuaDlrqQyMDblrqQe5Lit5Yy65Zu+5Lmm6aaG6Ieq5Lmg5a6kMjEx5a6kHuilv+WMuuWbvuS5pummhuiHquS5oOWupDIwN+WupB7opb/ljLrlm77kuabppoboh6rkuaDlrqQ0MDHlrqQe6KW/5Yy65Zu+5Lmm6aaG6Ieq5Lmg5a6kNDA45a6kFQgHMTQwMTAwMQcyMTAxMDAxBzIyMDEwMDEHMjIwNjAwMQcyMjExMDAxBzMyMDcwMDEHMzQwMTAwMQczNDA4MDAxFCsDCGdnZ2dnZ2dnFgECBWQCCQ8PFgIeBFRleHQFBjc4LzI0NGRkZIJLRlSaEzl+hXYbMzsabmNO5M8kN4OxvGABm9Fy2LY8',

'__EVENTVALIDATION':"/wEWEwL72v/5BALs0bLrBgLs0fbZDAKPwd2MAQLpu8rPDwKt6srPDwLytq6nDwK6p5TSCwKVpujRCwKVpuzQCwKVpvCRCAKetsSdCgL0p+TRCAL0p5TSCwL0p8CRDwL9h9mtDgKM54rGBgK7q7GGCALWlM+bAudkkeCZYjIbgxcot/UJfplpDini+B9RpiIThosUANI+",

'TextBox1':"883",

'TextBox2':"1920",

"ddlDay":"明日",

'ddlRoom':"3207001",

"txtSeats":"",

"Button1":"手动选座",

}

resp_1 = requests.post(churl, headers=headers, data=form_1, cookies=cookies)

except:

print("####---------登陆失败--------#######")

###提交预约座位

def chseat():

churl = "http://172.16.47.84/AppSTod.aspx?roomid=3207&hei=834&wd=1920"

churl_1= "http://172.16.47.84/Skip.aspx?seatid=3207128"

from_2 = {

'roomid' :'3207',

'hei' :'883',

'wd' :'1920',

}

from_3 = {

'seatid':'3207128',

}

for i in range(0,2):

resp_2 = requests.post(churl,headers = headers,data = from_2,cookies = cookies)

resp_3 = requests.post(churl_1,headers = headers,data = from_3,cookies = cookies)

soup = BeautifulSoup(resp_3.content,'lxml')

Soup = soup.find_all("script")

if Soup:

print(Soup[0])

else:

print("正在抢座")

for i in range(0,50):

time.sleep(1) #为防止服务器瘫痪,请求50次,这个大家可以根据需要,自行改动

resp_2 = requests.post(churl,headers = headers,data = from_2,cookies = cookies)

resp_3 = requests.post(churl_1,headers = headers,data = from_3,cookies = cookies)

检测是否成功抢座,是根据预约成功后弹出一个页面,告诉“你已经成功预约”, 而这句话是存在于

给大家看下结果:

f9e34f6a86b148869c22889fab3fe827

运行结果.PNG

登陆网站验证:

540e02babb7d4f44bf8a328140b038a8

验证结果.PNG

小编还在成长,希望大家多多指教。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/174000
推荐阅读
相关标签
  

闽ICP备14008679号