python网络爬虫系列(九)——打码平台的使用

python⽹络爬⾍系列(九)——打码平台的使⽤
验证码处理
学习⽬标
1. 了解 验证码的相关知识
2. 掌握 图⽚识别引擎的使⽤
3. 了解 常见的打码平台
4. 掌握 通过打码平台处理验证码的⽅法
1.图⽚验证码
1.1 什么是图⽚验证码
验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全⾃动区分计算机和⼈类的图灵测试)的缩写,是⼀种区分⽤户是计算机还是⼈的公共全⾃动程序。
1.2 验证码的作⽤
防⽌恶意破解密码、刷票、论坛灌⽔、刷页。有效防⽌某个⿊客对某⼀个特定注册⽤户⽤特定程序暴⼒破解⽅式进⾏不断的登录尝试,实际上使⽤验证码是现在很多⽹站通⾏的⽅式(⽐如招商银⾏的⽹上个⼈银⾏,百度社区),我们利⽤⽐较简易的⽅式实现了这个功能。虽然登录⿇烦⼀点,但是对⽹友的密码安全来说这个功能还是很有必要,也很重要。
1.3 图⽚验证码在爬⾍中的使⽤场景
注册
登录
频繁发送请求时,服务器弹出验证码进⾏验证
1.4 图⽚验证码的处理⽅案
化学浆糊
⼿动输⼊(input)
这种⽅法仅限于登录⼀次就可持续使⽤的情况
图像识别引擎解析
使⽤光学识别引擎处理图⽚中的数据,⽬前常⽤于图⽚数据提取,较少⽤于验证码处理
打码平台
爬⾍常⽤的验证码解决⽅案
2.图⽚识别引擎
OCR(Optical Character Recognition)是指使⽤扫描仪或数码相机对⽂本资料进⾏扫描成图像⽂件,然后对图像⽂件进⾏分析处理,⾃动识别获取⽂字信息及版⾯信息的软件。
2.1 什么是tesseract
Tesseract,⼀款由HP实验室开发由Google维护的开源OCR引擎,特点是开源,免费,⽀持多语⾔,多平台。
项⽬地址:
下载地址:
参考博⽂:
2.2 图⽚识别引擎环境的安装
激光快速成型机
1 引擎的安装
mac环境下直接执⾏命令
brew install --with-training-tools tesseract
windows环境下的安装
可以通过exe安装包安装,下载地址可以从GitHub项⽬中的wiki到。安装完成后记得将Tesseract 执⾏⽂件的⽬录加⼊到PATH中,⽅便后续调⽤。
苹果削皮机linux环境下的安装
sudo apt-get install tesseract-ocr
2 Python库的安装
# PIL⽤于打开图⽚⽂件
pip/pip3 install pillow
# pytesseract模块⽤于从图⽚中解析数据
pip/pip3 install pytesseract
2.3 图⽚识别引擎的使⽤
通过pytesseract模块的 image_to_string ⽅法就能将打开的图⽚⽂件中的数据提取成字符串数据,具体⽅法如下
from PIL import Image
import pytesseract
# 打开⼀个图⽚⽂件
im = Image.open('test2.png')
result2 = pytesseract.image_to_string(im)
print(result2)
2.4 图⽚识别引擎的使⽤扩展
其他ocr平台
微软Azure 图像识别:[azure.microsoft/zh-cn/services/cognitive-services/computer-vision/](azure.microsoft/zh-cn/services/cognitiv e-services/computer-vision/)
有道智云⽂字识别:[udao/ocrdemo](udao/ocrdemo)
阿⾥云图⽂识别:[www.aliyun/product/cdi/](www.aliyun/product/cdi/)
绿豆肽腾讯OCR⽂字识别:[t/product/ocr](t/product/ocr)
3 打码平台
1.为什么需要了解打码平台的使⽤
现在很多⽹站都会使⽤验证码来进⾏反爬,所以为了能够更好的获取数据,需要了解如何使⽤打码平台爬⾍中的验证码
2 常见的打码平台
1. 能够解决通⽤的验证码识别
2. 能够解决复杂验证码的识别
3 云打码的使⽤
下⾯以云打码为例,了解打码平台如何使⽤
3.1 云打码官⽅接⼝
下⾯代码是云打码平台提供,做了个简单修改,实现了两个⽅法:
1. indetify:传⼊图⽚的响应⼆进制数即可
2. indetify_by_filepath:传⼊图⽚的路径即可识别
其中需要⾃⼰配置的地⽅是:
username = 'whoarewe' # ⽤户名
password = '***' # 密码
appid = 4283 # appid
发光标识appkey = '02074c64f0d0bb9efb2df455537b01c3' # appkey
codetype = 1004 # 验证码类型
云打码官⽅提供的api如下:
#yundama.py
import requests
import json
import time
class YDMHttp:
apiurl ='api.yundama/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 = quest(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 = quest(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 = quest(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 = quest(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 = sult(cid)
if(result !=''):
return cid, result
else:
time.sleep(1)
return-3003,''
else:
return cid,''
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)
username ='whoarewe'# ⽤户名
password ='***'# 密码
appid =4283# appid
竹炭颗粒appkey ='02074c64f0d0bb9efb2df455537b01c3'# appkey
filename ='getimage.jpg'# ⽂件位置
codetype =1004# 验证码类型
# 超时
timeout =60
def indetify(response_content):
if(username =='username'):
print('请设置好相关参数再测试')
else:
# 初始化
yundama = YDMHttp(username, password, appid, appkey)
# 登陆云打码
uid = yundama.login();
print('uid: %s'% uid)

本文发布于:2024-09-21 16:33:30,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/260794.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:验证码   识别   引擎   打码   安装
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议