requests
,time
,json
HttpCanary
打开抓包工具HttpCanary
,登录青马易站,先答一道题(我选了A)。
抓包结果如下
POST /yiban-web/stu/nextSubject.jhtml?_=1602077251646 HTTP/1.1
Host: qm.linyisong.top
Connection: keep-alive
Content-Length: 10
Accept: application/json
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Linux; Android 10; HLK-AL00 Build/HONORHLK-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.108 Mobile Safari/537.36 yiban_android
Content-Type: application/x-www-form-urlencoded
Origin: http://qm.linyisong.top
Referer: http://qm.linyisong.top/yiban-web/stu/toSubject.jhtml?courseId=9
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9,en-CN;q=0.8,en;q=0.7,ja-JP;q=0.6,ja;q=0.5,en-US;q=0.4
Cookie: 这个填自己的
courseId=9
POST /yiban-web/stu/changeSituation.jhtml?_=1602588874479 HTTP/1.1
Host: qm.linyisong.top
Connection: keep-alive
Content-Length: 69
Accept: application/json
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Linux; Android 10; HLK-AL00 Build/HONORHLK-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.108 Mobile Safari/537.36 yiban_android
Content-Type: application/x-www-form-urlencoded
Origin: http://qm.linyisong.top
Referer: http://qm.linyisong.top/yiban-web/stu/toSubject.jhtml?courseId=2
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9,en-CN;q=0.8,en;q=0.7,ja-JP;q=0.6,ja;q=0.5,en-US;q=0.4
Cookie: 这个填自己的
answer=A&courseId=2&uuid=584FA4568509426D9CCE78FE7441C6F0&deviceUuid=
得到url后去浏览器中尝试访问
得到了这样的结果,很明显是User-Agent
不对,然后更改headers
。
更改过之后会出现
浏览器能够访问之后,就可以在python中进行操作了。
获取题目
data里面信息只有courseId
,选中的科目不同,courseId
会有不同的值
url = 'http://qm.linyisong.top/yiban-web/stu/nextSubject.jhtml?_={}'.format(getTime())
referer = 'http://qm.linyisong.top/yiban-web/stu/toSubject.jhtml?courseId={}'.format(self)
headers = {
'Referer': referer,
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; HLK-AL00 Build/HONORHLK-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.108 Mobile Safari/537.36 yiban_android',
'Cookie': '这里写进去自己的Cookie',
'Accept': 'application/json',
'Origin': 'http://qm.linyisong.top',
'Host': 'qm.linyisong.top'
}
data = {
'courseId': self
}
req = requests.post(url, headers=headers, data=data)
html = json.loads(req.text)
提交答案
如果缺少uuid
会出现错误,在前一个的包中,能发现它返回了一个uuid
,正好作为回答题目的标识。
data_ = {
'answer': 'A',
'courseId': self,
'uuid': UUID
}
req_ = requests.post(url_, headers=headers, data=data_)
res_ = json.loads(req_.text)
注意
url
后边接了一个时间戳,如果缺少会出现错误。
import requests
import time
import json
# 获取时间戳
def getTime():
t = str(time.time())
a = str(t[0:10])
b = str(t[11:14])
res = a + b
return res
# 核心代码
def function(self):
referer = 'http://qm.linyisong.top/yiban-web/stu/toSubject.jhtml?courseId={}'.format(self)
url_ = 'http://qm.linyisong.top/yiban-web/stu/changeSituation.jhtml?_={}'.format(getTime())
url = 'http://qm.linyisong.top/yiban-web/stu/nextSubject.jhtml?_={}'.format(getTime())
headers = {
'Referer': referer,
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; HLK-AL00 Build/HONORHLK-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.108 Mobile Safari/537.36 yiban_android',
'Cookie': '这里写进去自己的Cookie',
'Accept': 'application/json',
'Origin': 'http://qm.linyisong.top',
'Host': 'qm.linyisong.top'
}
data = {
'courseId': self
}
req = requests.post(url, headers=headers, data=data)
html = json.loads(req.text)
# 获取当前题目的标识
UUID = html['data']['uuid']
print(html['data']['nextSubject'])
# answer为当前题目提交的答案
data_ = {
'answer': 'A',
'courseId': self,
'uuid': UUID
}
req_ = requests.post(url_, headers=headers, data=data_)
res_ = json.loads(req_.text)
print(res_)
if __name__ == "__main__":
id = input("课程的Id: ")
function(id)
{'courseId': 2, 'option0': '对', 'option1': '错', 'optionCount': 2, 'score': 1, 'subDescript': '中国经济因结构调整和受外部环境恶化影响,自 2013 年上半年增速明显提速。', 'subType': '单选题', 'timeLimit': 60}
{'data': {'rightAnswer': False, 'rightOption': 'B.错'}, 'message': '回答错误!', 'isSuccess': True}