昨天浏览网站的时候看到一个帖子,大意是如何用python去模拟网站进行成绩查询,于是有了这个帖子
先把代码放上来
# coding: utf-8
import requests
session = requests.session()
headers = {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'DNT': '1',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Origin': 'https://411318.yichafen.com',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Referer': 'https://411318.yichafen.com/public/queryscore/sqcode/MsTcIn3mOTU1MnxkOGFiNDUzMWJjOGY3MjhiY2ZkN2FiZmQ0MmE0YzgxOXw0MTEzMTgO0O0O.html',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
}
def get_cookies():
response = session.get(url='https://411318.yichafen.com', headers=headers)
print(response.cookies)
def get_score():
response = session.get('https://411318.yichafen.com/public/queryresult.html', headers=headers)
print(response.text)
print(response.headers)
def get_status(usercode='101106120448', username='肖涛'):
data = {
's_kaohao': usercode,
's_xingming': username
}
response = session.post(
'https://411318.yichafen.com/public/checkcondition/sqcode/MsTcIn3mOTU1MnxkOGFiNDUzMWJjOGY3MjhiY2ZkN2FiZmQ0MmE0YzgxOXw0MTEzMTgO0O0O.html',
headers=headers, data=data)
print(response.text)
def main():
get_cookies()
get_status()
get_score()
if __name__ == '__main__':
main()
接着说说我的分析过程
-
请求分为三步,第一步是拿到cookie,第二步提交用户名和考号会得到一个返回结果,如下
<pre>{"info":"\u67e5\u8be2\u6210\u529f","status":1,"url":"\/public\/queryresult.html"}</pre>
-
显示请求成功,但是还要进行第三步,第三部必须带上referer,否者不返回结果
-
最重要的是访问必须携带cookie而且要在同一个session中,否则不返回数据,所以代码中第一部是请求cookies,用session自动保存cookies,避免了cookie的管理
|