3-爬虫-模拟登录图片验证码处理
Posted wgwg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3-爬虫-模拟登录图片验证码处理相关的知识,希望对你有一定的参考价值。
模拟登录
- 验证码的识别
- 线上的打码平台
- 超级鹰
- url:https://www.chaojiying.com/about.html
- 使用流程:
- 注册:注册一个用户中心的账号
- 登录:用户中心的身份
- 创建一个软件ID: 899370
- 下载示例代码
- 云打码
- 超级鹰
- 线上的打码平台
- 动态变化的请求参数
- 动态变化请求参数的处理
- 一般会隐藏在前台页面中
- 是由相关的js函数动态生成
- 动态变化请求参数的处理
超级鹰的示例代码
#!/usr/bin/env python # coding:utf-8 import requests from hashlib import md5 class Chaojiying_Client(object): def __init__(self, username, password, soft_id): self.username = username password = password.encode(‘utf8‘) self.password = md5(password).hexdigest() self.soft_id = soft_id self.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 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() if __name__ == ‘__main__‘: chaojiying = Chaojiying_Client(‘超级鹰用户名‘, ‘超级鹰用户名的密码‘, ‘96001‘) #用户中心>>软件ID 生成一个替换 96001 im = open(‘a.jpg‘, ‘rb‘).read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要// print chaojiying.PostPic(im, 1902) #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
识别古诗文网中的验证码图片
import requests from lxml import etree headers = { ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36‘ } def transdform_code_img(img_path,img_type): chaojiying = Chaojiying_Client(‘超级鹰账号‘, ‘密码‘, ‘软件ID‘) #用户中心>>软件ID 生成一个替换 96001 im = open(img_path, ‘rb‘).read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要// return chaojiying.PostPic(im, img_type)[‘pic_str‘] main_url = ‘https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx‘ page_text = requests.get(url=main_url,headers=headers).text #解析验证码图片地址 tree = etree.HTML(page_text) img_src = ‘https://so.gushiwen.org‘+tree.xpath(‘//*[@id="imgCode"]/@src‘)[0] img_data = requests.get(url=img_src,headers=headers).content with open(‘./code.jpg‘,‘wb‘) as fp: fp.write(img_data) transdform_code_img(‘./code.jpg‘,1004)
实现模拟登录
sess = requests.Session() main_url = ‘https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx‘ page_text = sess.get(url=main_url,headers=headers).text #解析验证码图片地址 tree = etree.HTML(page_text) img_src = ‘https://so.gushiwen.org‘+tree.xpath(‘//*[@id="imgCode"]/@src‘)[0] img_data = sess.get(url=img_src,headers=headers).content with open(‘./code.jpg‘,‘wb‘) as fp: fp.write(img_data) __VIEWSTATE = tree.xpath(‘//*[@id="__VIEWSTATE"]/@value‘)[0] __VIEWSTATEGENERATOR = tree.xpath(‘//*[@id="__VIEWSTATEGENERATOR"]‘)[0] code_text = transdform_code_img(‘./code.jpg‘,1004) print(code_text) login_url = ‘https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx‘ data = { ‘__VIEWSTATE‘: __VIEWSTATE, ‘__VIEWSTATEGENERATOR‘: __VIEWSTATEGENERATOR, ‘from‘: ‘http://so.gushiwen.org/user/collect.aspx‘, ‘email‘: ‘账号‘, ‘pwd‘: ‘密码‘, ‘code‘: code_text, ‘denglu‘: ‘登录‘, } login_page_text = sess.post(url=login_url,headers=headers,data=data).text with open(‘./login.html‘,‘w‘,encoding=‘utf-8‘) as fp: fp.write(login_page_text)
以上是关于3-爬虫-模拟登录图片验证码处理的主要内容,如果未能解决你的问题,请参考以下文章
Selenium&Pytesseract模拟登录+验证码识别