本文主要是介绍基于古诗文网获取验证码并实现模拟登录,解决验证码错误问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这里用的云打码平台为超级鹰,小伙伴们可以自行注册充值1毛钱做测试,下面附上代码
"""
@Author:Acoit
@File:古诗文网模拟登录.py
@Time:2022/12/2 19:55
"""
import requests
from lxml import etree
from chaojiying import Chaojiying_Clientif __name__ == '__main__':session = requests.Session()# 古诗文网登录页面urlget_url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'# 模拟登录请求头get_header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0'}# 请求url获取textpage_text = requests.get(url=get_url, headers=get_header).text# 解析HTMLtree = etree.HTML(page_text)# 定位src属性并拼接获得最终验证码urlsrc_url = 'https://so.gushiwen.cn' + tree.xpath('//*[@id="imgCode"]/@src')[0]# 请求验证码url获取二进制src_data = session.get(url=src_url, headers=get_header).content# 将获取的的二进制数据写入到当前目录下获得code.jpgwith open('./code.jpg', 'wb') as fp:fp.write(src_data)# 用户中心>>软件ID 生成一个替换 96001chaojiying = Chaojiying_Client('xxoo123', 'xxoo123..', '942803')# 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//im = open('./code.jpg', 'rb').read()# 古诗文网登录所需的验证码类型为1004 验证码类型src = chaojiying.PostPic(im, 1004)# print(chaojiying.PostPic(base64_str, 1902)) # 此处为传入 base64代码# 验证码codepic_str = src['pic_str']print(pic_str)# 模拟登录__VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0]__VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0]login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'data = {'__VIEWSTATE': __VIEWSTATE,'__VIEWSTATEGENERATOR': __VIEWSTATEGENERATOR,'from': 'http://so.gushiwen.cn/user/collect.aspx','email': 'xxoo123@163.com','pwd': 'xxoo123..','code': pic_str,'denglu': '登录',}gushiwen = session.post(url=login_url,headers=get_header,data=data).textwith open('./gushiwen.html','w',encoding='utf-8') as fp_h:fp_h.write(gushiwen)
这里的chaojiying模块是超级鹰下载的python包,小伙伴们可自行下载并修改,这里我附上我的:
#!/usr/bin/env python
# coding:utf-8import requests
from hashlib import md5class Chaojiying_Client(object):def __init__(self, username, password, soft_id):self.username = usernamepassword = password.encode('utf8')self.password = md5(password).hexdigest()self.soft_id = soft_idself.base_params = {'user': self.username,'pass2': self.password,'softid': self.soft_id,}self.headers = {'Connection': 'Keep-Alive','User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',}def PostPic(self, im, codetype):"""im: 图片字节codetype: 题目类型 参考 http://www.chaojiying.com/price.html"""params = {'codetype': codetype,}params.update(self.base_params)files = {'userfile': ('ccc.jpg', im)}r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files,headers=self.headers)return r.json()def PostPic_base64(self, base64_str, codetype):"""im: 图片字节codetype: 题目类型 参考 http://www.chaojiying.com/price.html"""params = {'codetype': codetype,'file_base64': base64_str}params.update(self.base_params)r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, headers=self.headers)return r.json()def ReportError(self, im_id):"""im_id:报错题目的图片ID"""params = {'id': im_id,}params.update(self.base_params)r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)return r.json()
这篇关于基于古诗文网获取验证码并实现模拟登录,解决验证码错误问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!