import requests import json from datetime import datetime import base64 from lxml import etree url = "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx" headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69" } def common_verify(image): # 数英汉字类型 # 通用数英1-4位 10110 # 通用数英5-8位 10111 # 通用数英9~11位 10112 # 通用数英12位及以上 10113 # 通用数英1~6位plus 10103 # 定制-数英5位~qcs 9001 # 定制-纯数字4位 193 # 中文类型 # 通用中文字符1~2位 10114 # 通用中文字符 3~5位 10115 # 通用中文字符6~8位 10116 # 通用中文字符9位及以上 10117 # 中文字符 1~4位 plus 10118 # 定制-XX西游苦行中文字符 10107 # 计算类型 # 通用数字计算题 50100 # 通用中文计算题 50101 # 定制-计算题 cni 452 with open(image, "rb") as f: s = f.read() _custom_url = "http://api.jfbym.com/api/YmServer/customApi" _token = "云码平台个人中心获取token" _headers = { 'Content-Type': 'application/json' } payload = { "image": base64.b64encode(s).decode(), "token": _token, "type": "10110" } resp = requests.post(_custom_url, headers=_headers, data=json.dumps(payload)) print(resp.text) return resp.json()['data']['data'] def code_image_dispose(): page_text = requests.get(url=url, headers=headers).text tree = etree.HTML(page_text) # class定位 img_url = "https://so.gushiwen.org" + tree.xpath("//div[@class='mainreg2']/div/img/@src")[0] # id定位 # img_url = "https://so.gushiwen.org"+tree.xpath("//*[@id='imgCode']/@src")[0] img_date = requests.get(url=img_url, headers=headers).content img_path = f"./验证码-{str(datetime.now())}.jpg" with open(img_path, "wb") as fp: fp.write(img_date) print("验证码下载完成!!!") return img_path def login(code): url = "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx" headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69" } param = { "__VIEWSTATE": "SKbAEdRDWUnaRTSumtFAV9HJtljHcbPgf3WfyI2dD898llqpaSWJYiW6F10ju6c5AxPwt7Cu0W5ayRqIJg2NNM59muUrUrzZUMFnvQg6JoUZcmZIu9c0gIaaBsI+RYiAH/wa1Imb6wjUhTTuMRz8aRsXmPw=", "__VIEWSTATEGENERATOR": "C93BE1AE", "from": "http://so.gushiwen.cn/user/collect.aspx", "email": "个人账号, "pwd": "密", "code": code, "denglu": "登录" } response = requests.get(url=url, params=param, headers=headers) print(response.status_code) if __name__ == '__main__': imgPath = code_image_dispose() a = common_verify(imgPath) login(a)