会有true字符串或者false,那么我就可以利用这个来验证我是不是正确
这样用到的re库 需要导入
回到刚才的登陆
继续看抓包的数据,发现了post方法,一般来说post方法是真正的登陆窗口
post方法前面的 get /xsxk/loadDate...... 就是我们前面实现的get,现应该实现post真正的登陆了
在查看post发送的数据中包含了头信息,其中包括cookie 和账号密码所以我们用requests的post方法继续伪造请求
在post结束后学校服务器会返回一个302 found的http状态码 这表示浏览器将我的数据发送后,会重新跳转到另外一个网页
不过requests是可以自动跳转的,不需要担心
代码
log_header={ 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Origin':'http://xk.qhnu.edu.cn',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36',
'Referer':'http://xk.qhnu.edu.cn/xsxk/logout.xk',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.9',
'Host': 'xk.qhnu.edu.cn',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1'
}
date={
'username':自己的学号,
'password':自己的密码,
}
log_url='http://xk.qhnu.edu.cn/xsxk/login.xk'
r=requests.post(log_url,data=date,headers=log_header,cookies=cookie)
#传递的参数cooki由之前的代码获得
其中的 date和log_header都是通过抓包看到的 这个网页需要哪些数据。
代码写好后 运行并且抓包发现 与用网页登陆抓取的包大致相同,通过多次修改可以总结如下
通过用网页登陆抓包发现登陆学校的网址有一下步骤
1.在清空缓存,刷新时(即第一次登陆时),会先向xsxk/logout.xk发送cookie
2.在get验证码的所需要的数据中 通过抓包发现需要用到第一步的cookie
3.在登陆前需要向xsxk/loadData.xk发送自己的账号密码和验证码和cookie
4.登陆时用的是post方法,并且还需要将自己的账号密码和之前的验证码和cookie发送一次 如果没有完成第三步那么无论怎么登陆都失败
完整代码如下
import time
import requests
import re
from bs4 import BeautifulSoup #获取解析网页的库
username=input("账号")
password=input("密码")
get_cookie_url='http://xk.qhnu.edu.cn/xsxk/logout.xk'
jpg_url = 'http://xk.qhnu.edu.cn/xsxk/servlet/ImageServlet?d='+str(int(time.time())*1000)
check_url='http://xk.qhnu.edu.cn/xsxk/loadData.xk'
log_url='http://xk.qhnu.edu.cn/xsxk/login.xk'
get_cookie_header={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36'
}
check_header={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36',
'Host': 'xk.qhnu.edu.cn',
}
params={
'method':'checkLogin',
'username':username,
'password':password,
}
log_header={
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Origin':'http://xk.qhnu.edu.cn',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36',
'Referer':'http://xk.qhnu.edu.cn/xsxk/logout.xk',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.9',
'Host': 'xk.qhnu.edu.cn',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1'
}
date={
'username':username,
'password':password,
}
r = requests.get(get_cookie_url)
cookie=r.cookies.get_dict()
#print(cookie) 输出cookie
content = requests.get(jpg_url,cookies=cookie).content
with open('demo.jpg', 'wb') as fp:
fp.write(content)
while(True):
code = input('请输入验证码:')
datacode= str(code)
params['verifyCode']=datacode
r = requests.get(check_url,params=params,cookies=cookie)
if re.search('true',r.text):
print (