import json
import requests
from lxml import etree
from chaojiying import Chaojiying_Client
#模拟古诗文登录
#登陆页面url
url = "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx"
header = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.54"
}
session = requests.Session()
reso = session.get(url,headers=header).text
e = etree.HTML(reso)
#获取图片url
image_url =e.xpath('//*[@id="imgCode"]/@src')
#获取动态参数
VIEWSTATE = e.xpath('//*[@id="__VIEWSTATE"]/@value')[0]
VIEWSTATEGENERATOR = e.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0]
#验证码url
image_url ='https://so.gushiwen.cn' + "".join(image_url)
image = session.get(image_url,headers=header).content
#保存图片道本地
with open("image.jpg","wb") as f:
f.write(image)
#识别验证码
chaojiying = Chaojiying_Client('hjc2001', 'HHH2001911', '952004')
im = open('image.jpg', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print(chaojiying.PostPic(im, 1104)['pic_str'])
#设置上传表单内容
data = {
'__VIEWSTATE': VIEWSTATE,
'__VIEWSTATEGENERATOR': VIEWSTATEGENERATOR,
'from': 'http://so.gushiwen.cn/user/collect.aspx',
'email': '154143942@qq.com',
'pwd': 'HHH2001911',
'code': chaojiying.PostPic(im, 1104)['pic_str'],
'denglu': '登录'
}
login_resp = session.post(url,headers=header,data=data)
with open('main.html','w',encoding='utf-8',) as f:
f.write(login_resp.text)
爬虫模拟登录古诗文网站
最新推荐文章于 2024-07-05 17:19:57 发布