04 验证码识别
04 验证码识别
what is 验证码?
是一种区分用户是计算机还是人的公共全自动程序。验证码可以防止:恶意破解密码、刷票、论坛灌水,有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试,实际上用验证码是现在很多网站通行的方式,我们利用比较简易的方式实现了这个功能。
验证码和爬虫的爱恨情仇
有些时候,我们想要去爬取一些基于某些用户的用户信息,比如爬取张三新浪微博的博文和好友数量……那么这些时候,我们就需要使用爬虫进行某些平台的模拟登陆,登陆成功后爬取该用户的相关用户信息。模拟登陆的过程中,往往会出现验证码的阻挠,并且我们还不可以不处理验证码,否则是不可能实现模拟登陆的。因此,接下来我们就一起来看一下,如何处理或者识别验证码。
云打码平台
处理验证码,最通用的方式就是使用三方平台帮我们对相关的验证码进行识别和处理。其中云打码平台就是一个非常通用的打码平台。
http://www.yundama.com/demo.html
该平台可以帮助我们识别的验证码有如下类型:
使用流程
- 云打码平台:
- 在官网中进行注册(普通用户和开发者用户)
- 登录开发者用户:
- 实例代码的下载(开发文档-》调用示例及最新的DLL-》PythonHTTP实例下载)
- 创建一个软件:我的软件-》添加新的软件
- 使用示例代码中的源码文件中的代码进行修改,让其识别验证码图片中的数据值
案例:识别验证码 古诗文网
#YunCode.PY import http.client, mimetypes, urllib, json, time, requests ###################################################################### import http.client, mimetypes, urllib, json, time, requests class YDMHttp: apiurl = 'http://api.yundama.com/api.php' username = '' password = '' appid = '' appkey = '' def __init__(self, username, password, appid, appkey): self.username = username self.password = password self.appid = str(appid) self.appkey = appkey def request(self, fields, files=[]): response = self.post_url(self.apiurl, fields, files) response = json.loads(response) return response def balance(self): data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey} response = self.request(data) if (response): if (response['ret'] and response['ret'] < 0): return response['ret'] else: return response['balance'] else: return -9001 def login(self): data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey} response = self.request(data) if (response): if (response['ret'] and response['ret'] < 0): return response['ret'] else: return response['uid'] else: return -9001 def upload(self, filename, codetype, timeout): data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)} file = {'file': filename} response = self.request(data, file) if (response): if (response['ret'] and response['ret'] < 0): return response['ret'] else: return response['cid'] else: return -9001 def result(self, cid): data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid)} response = self.request(data) return response and response['text'] or '' def decode(self, filename, codetype, timeout): cid = self.upload(filename, codetype, timeout) if (cid > 0): for i in range(0, timeout): result = self.result(cid) if (result != ''): return cid, result else: time.sleep(1) return -3003, '' else: return cid, '' def report(self, cid): data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'} response = self.request(data) if (response): return response['ret'] else: return -9001 def post_url(self, url, fields, files=[]): for key in files: files[key] = open(files[key], 'rb'); res = requests.post(url, files=files, data=fields) return res.text def get_code_image(filename,code_type): ''' 调用次函数可识别验证码 :param filename: 图片的本地路径 :param code_type: 识别验证码的类型,可见云打码平台 :return: ''' result =None # 用户名 username = 'xxx' # 密码 password = 'xxx...' # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得! appid = 10303 # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得! appkey = '5b435fc2b6666946f84d6cee324075f9' # 图片文件 filename = filename # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html codetype = code_type # 超时时间,秒 timeout = 20 # 检查 if (username == 'username'): print('请设置好相关参数再测试') else: # 初始化 yundama = YDMHttp(username, password, appid, appkey) # 登陆云打码 uid = yundama.login(); print('uid: %s' % uid) # 查询余额 balance = yundama.balance(); print('balance: %s' % balance) # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果 cid, result = yundama.decode(filename, codetype, timeout); print('cid: %s, result: %s' % (cid, result)) return result
#识别验证码.py import requests from lxml import etree from YunCode import YDMHttp,get_code_image headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36' } url = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx' page_text = requests.get(url=url,headers=headers).text #解析验证码图片地址 tree = etree.HTML(page_text) code_img_src = 'https://so.gushiwen.org'+tree.xpath('//*[@id="imgCode"]/@src')[0] #请求验证码图片,且保存至本地 img_data = requests.get(url=code_img_src,headers=headers).content with open('./code.jpg','wb') as fp: fp.write(img_data) get_code_image('./code.jpg',1004)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)