import requests
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/125.0.0.0 Safari/537.36 Edg/125.0.0.0'
}
# __VIEWSTATE: uM9grpiBSD3Q114jDI+CaG+nXuQ7qAbawhd0nPQ5pfb9tC1FTEsp4PuKrc4ZHMKgPVGsWdkzSe4ipKPS7o3urFD8KAFjvtoP1N1b7Tj23vuIUrtLcDx5cat3fyZbA+tslYEaIJySr4S0JimvjObL2qDJaio=
# __VIEWSTATEGENERATOR: C93BE1AE
# from: https://www.gushiwen.cn/
# email: 15939014979 已知
# pwd: liuyihao123123 已知
# code: 07Fk 未知
# denglu: 登录
# 未知的: __VIEWSTATE __VIEWSTATEGENERATOR 前边这俩去源码里找 code
response = requests.get(url=url, headers=headers)
content = response.text
from lxml import etree
tree = etree.HTML(content)
# 获取VIEWSTATE
viewstate = tree.xpath('//div/input/@value')[0]
# print(viewstate)
viewstategenerator = tree.xpath('//div/input/@value')[1]
# print(viewstategenerator)
code = tree.xpath('//div/img/@src')[1]
# print(code)
code_url = "https://www.gushiwen.cn" + code
# print(code_url)
# 用这个的话在下面输入验证码时已经变了
# import urllib.request
# urllib.request.urlretrieve(url=code_url, filename='code.jpg')
# requests里边有一个方法session()通过session的返回值就能使请求变成一个对象
session = requests.session()
# 验证码的url的内容
response_code = session.get(code_url)
# 注意这里要用二进制数据,因为我们要的是图片下载
content_code = response_code.content
# wb的模式就是将二进制的数据写入文件
with open('code.jpg', 'wb') as fp:
fp.write(content_code)
# 获取了验证码的图片之后 下载到本地 观察之后在控制台输入这个东西
code_name = input("请输入你的验证码")
url_post = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data_post = {
'__VIEWSTATE': viewstate,
'__VIEWSTATEGENERATOR': viewstategenerator,
'from': "https://www.gushiwen.cn/",
'email': "15939014979",
'pwd': "liuyihao123",
'code': code_name,
'denglu': '登录',
}
response_post = session.post(url=url, headers=headers, data=data_post)
content_post = response_post.text
with open('gushiwen.html', 'w', encoding=' utf-8') as fp:
fp.write(content_post)
输入验证码后显示错误,不清楚