`# 模拟古诗文网的登录
import requests
from datetime import datetime
from lxml import etree
import base64
import time
login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
requestLoginUrl = r"https://so.gushiwen.org/user/login.aspx"
prefixCode = r"https://www.gushiwen.cn/"
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
}
randomFactor = str(datetime.timestamp(datetime.today())).split(".")[1]
def login() ->str:
session = requests.Session()
response = session.get(login_url, headers= header).text
tree = etree.HTML(response)
codeSuffix = tree.xpath('//*[@id="imgCode"]/@src')[0]
codeByteData = session.get('https://so.gushiwen.cn/'+codeSuffix, headers=header).content
codeImageABS = "G:/shark_net/img/{0}.jpg".format(randomFactor)
view_state = str(tree.xpath('//input[@name="__VIEWSTATE"]/@value')[0])
viewstategeneral = str(tree.xpath('//input[@name="__VIEWSTATEGENERATOR"]/@value')[0])
# b = base64.b64decode(codeByteData.decode)
# print(type(b))
# with open()
# 验证码可以不进行存储,而是直接进行识别,其实也可以进行验证码图片的备份或者其他用途
# !!!验证码的识别接口,需要将验证码图片转换为 二进制base64字符串, 可优化: 无需进行验证码存储,而直接转换给接口调用
with open(codeImageABS, mode="wb") as inf:
inf.write(codeByteData)
with open(codeImageABS, 'rb') as f:
b = base64.b64encode(f.read()).decode() ## 图片二进制流base64字符串
# 验证码接口:验证码识别
url = "http://api.jfbym.com/api/YmServer/customApi"
data = {
# 关于参数,一般来说有3个;不同类型id可能有不同的参数个数和参数名,找客服获取
"token": "mZv2dZmAAmVJblLUC7MGVf4WoEOAB2gAzhL7ahtopAY",
"type": "10103",
"image": b
}
_headers = {
"Content-Type": "application/json"
}
response = session.request("POST", url, headers=_headers, json=data).json()
# print(codeImageABS)
# print(response)
print(response)
# return response["data"]["data"]
codeText = str(response["data"]["data"])
print(codeText)
data = {
"__VIEWSTATE":view_state,
"__VIEWSTATEGENERATOR":viewstategeneral,
"from":"https://www.gushiwen.cn/user/login.aspx",
"email":"????",
"pwd":"???",
"code":codeText,
"denglu":"登录"
}
response = session.post(url= requestLoginUrl, headers= header, data= data)
print(response.status_code)
print(data)
loginPageText = response.text
with open("gushiwen.html", "w", encoding= "utf-8") as f:
f.write(loginPageText)
if name=="main":
login()
print("验证码图片文件:", randomFactor)
`
以上为全局coding, 但是打印登录后的页面会出现错误 “ (需启用cookie保存登录状态) ”,
还请大佬答疑,问题在哪?