验证码处理

学习目标
  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引擎,特点是开源,免费,支持多语言,多平台。
  • 项目地址:https://github.com/tesseract-ocr/tesseract
  • 下载地址:https://digi.bib.uni-mannheim.de/tesseract/
  • 参考博文:https://blog.csdn.net/jacke121/article/details/76038663

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 图片识别引擎的使用扩展

  • tesseract简单使用与训练
  • 其他ocr平台
    微软Azure 图像识别:[https://azure.microsoft.com/zh-cn/services/cognitive-services/computer-vision/](https://azure.microsoft.com/zh-cn/services/cognitive-services/computer-vision/)有道智云文字识别:[http://aidemo.youdao.com/ocrdemo](http://aidemo.youdao.com/ocrdemo)阿里云图文识别:[https://www.aliyun.com/product/cdi/](https://www.aliyun.com/product/cdi/)腾讯OCR文字识别:[https://cloud.tencent.com/product/ocr](https://cloud.tencent.com/product/ocr)

3 打码平台

1.为什么需要了解打码平台的使用

现在很多网站都会使用验证码来进行反爬,所以为了能够更好的获取数据,需要了解如何使用打码平台爬虫中的验证码

2 常见的打码平台

  1. 云打码:http://www.yundama.com/

    能够解决通用的验证码识别

  2. 极验验证码智能识别辅助:http://jiyandoc.c2567.com/

    能够解决复杂验证码的识别

3 云打码的使用

下面以云打码为例,了解打码平台如何使用

3.1 云打码官方接口

下面代码是云打码平台提供,做了个简单修改,实现了两个方法:

  1. indetify:传入图片的响应二进制数即可
  2. indetify_by_filepath:传入图片的路径即可识别

其中需要自己配置的地方是:

username = 'whoarewe' # 用户名password = '***' # 密码appid = 4283 # appidappkey = '02074c64f0d0bb9efb2df455537b01c3' # appkeycodetype = 1004 # 验证码类型

云打码官方提供的api如下:

#yundama.py
import requests
import json
import timeclass YDMHttp:apiurl = 'http://api.yundama.com/api.php'username = ''password = ''appid = ''appkey = ''def __init__(self, username, password, appid, appkey):self.username = usernameself.password = passwordself.appid = str(appid)self.appkey = appkeydef request(self, fields, files=[]):response = self.post_url(self.apiurl, fields, files)response = json.loads(response)return responsedef 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 -9001def 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 -9001def 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 -9001def 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, resultelse: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)return res.text username = 'whoarewe' # 用户名password = '***' # 密码appid = 4283 # appidappkey = '02074c64f0d0bb9efb2df455537b01c3' # appkeyfilename = 'getimage.jpg' # 文件位置codetype = 1004 # 验证码类型# 超时
timeout = 60def indetify(response_content):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(response_content, codetype, timeout)print('cid: %s, result: %s' % (cid, result))return resultdef indetify_by_filepath(file_path):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(file_path, codetype, timeout)print('cid: %s, result: %s' % (cid, result))return resultif __name__ == '__main__':pass

4 常见的验证码的种类

4.1 url地址不变,验证码不变

这是验证码里面非常简单的一种类型,对应的只需要获取验证码的地址,然后请求,通过打码平台识别即可

4.2 url地址不变,验证码变化

这种验证码的类型是更加常见的一种类型,对于这种验证码,大家需要思考:

在登录的过程中,假设我输入的验证码是对的,对方服务器是如何判断当前我输入的验证码是显示在我屏幕上的验证码,而不是其他的验证码呢?

在获取网页的时候,请求验证码,以及提交验证码的时候,对方服务器肯定通过了某种手段验证我之前获取的验证码和最后提交的验证码是同一个验证码,那这个手段是什么手段呢?

很明显,就是通过cookie来实现的,所以对应的,在请求页面,请求验证码,提交验证码的到时候需要保证cookie的一致性,对此可以使用requests.session来解决


小结

  1. 了解 验证码的相关知识
  2. 掌握 图片识别引擎的使用
  3. 了解 常见的打码平台
  4. 掌握 通过打码平台处理验证码的方法

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

  1. python网络爬虫系列教程_Python网络爬虫系列教程连载 ----长期更新中,敬请关注!...

    感谢大家长期对Python爱好者社区的支持,后期Python爱好者社区推出Python网络爬虫系列教程.欢迎大家关注.以下系列教程大纲,欢迎大家补充.视频长期连载更新中 --------------- ...

  2. python网络爬虫系列教程——python中pyquery库应用全解

    全栈工程师开发手册 (作者:栾鹏) python教程全解 python网络爬虫lxml库的应用全解. 在线安装方法:cmd中输入"pip install pyquery" 离线安装 ...

  3. python网络爬虫系列教程——python中lxml库应用全解(xpath表达式)

    全栈工程师开发手册 (作者:栾鹏) python教程全解 python网络爬虫lxml库的应用全解. 在线安装方法:cmd中输入"pip install lxml" 离线安装,下载 ...

  4. python网络爬虫系列教程——python中requests库应用全解

    全栈工程师开发手册 (作者:栾鹏) python教程全解 python中requests库的基础应用,网页数据挖掘的常用库之一.也就是说最主要的功能是从网页抓取数据. 使用前需要先联网安装reques ...

  5. [Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析

    更新:感谢评论中朋友的提醒,百度贴吧现在已经改成utf-8编码了吧,需要把代码中的decode('gbk')改成decode('utf-8'). 百度贴吧的爬虫制作和糗百的爬虫制作原理基本相同,都是通 ...

  6. python网络爬虫系列(八)——常见的反爬手段和解决方法

    常见的反爬手段和解决思路 学习目标 了解 服务器反爬的原因 了解 服务器常反什么样的爬虫 了解 反爬虫领域常见的一些概念 了解 反爬的三个方向 了解 常见基于身份识别进行反爬 了解 常见基于爬虫行为进 ...

  7. python网络爬虫系列教程——python网络数据爬虫误区,让你的爬虫更像人类

    1 前言 近期,有些朋友问我一些关于如何应对反爬虫的问题.由于好多朋友都在问,因此决定写一篇此类的博客.把我知道的一些方法,分享给大家.博主属于小菜级别,玩爬虫也完全是处于兴趣爱好,如有不足之处,还望 ...

  8. python网络爬虫系列(0)——爬虫概述 http协议复习

    一.爬虫概述 知识点: 了解 爬虫的概念 了解 爬虫的作用 了解 爬虫的分类 掌握 爬虫的流程 1. 爬虫的概念 模拟浏览器,发送请求,获取响应 网络爬虫(又被称为网页蜘蛛,网络机器人)就是模拟客户端 ...

  9. Python网络爬虫系列(一)

    本节主要是体会Python进行网络爬虫的大概流程,代码部分需其它基础,后续会具体介绍. 一.为什么推荐Python进行网络爬虫 (1)上手容易 (2)免费开源,使用不受限制 (3)解释执行,跨平台不受 ...

最新文章

  1. android游戏开发者大会,第二届中国Android应用开发大会将开
  2. [BUUCTF-pwn]——inndy_rop
  3. app和外部应用的关联及应用举例
  4. Java中多线程的使用!!
  5. 喜讯,Asp.net Ajax 文档提供下载
  6. Spring Boot开发之流水无情(二)
  7. WordPress5.7版本下载及更新内容
  8. 计算正方形面积和周长_小学三年级数学下册长方形和正方形面积计算练习题(无答案)...
  9. 中国营销界:震惊全球的六种“武器”
  10. 数据分析看关晓彤的招黑之路
  11. java回调函数机制
  12. Eclipse, jsp代码修改之后,页面却没有变化!解决思路
  13. 嵌入式-----产品手册----塔吊黑匣子电气安装培训
  14. Android 事件拦截分发
  15. 冰点还原无法修改计算机时间,设置冰点还原解冻期间方法
  16. Spark入门之七:了解SparkSQL运行计划及调优
  17. Rosalind Python|Inferring mRNA from Protein
  18. 为什么 APISIX Ingress 是比 Traefik 更好的选择?
  19. ui设计线上培训怎么样?ui设计线上与线下的区别?
  20. heartbeat错误报告

热门文章

  1. mysql用supervisor管理_Supervisor使用详解
  2. 数据分析之pandas笔记
  3. 【Linux分享】Linux常用命令+教程分享
  4. 【R】语言第三课----矩阵
  5. python# 完成“剪刀石头布游戏”:
  6. linux su切换用户提示Authentication failture的解决办法
  7. ftp改为sftp_浅谈 FTP、FTPS 与 SFTP
  8. 如何在VMWare的Ubuntu虚拟机中设置共享文件夹
  9. java二级考试备考_2017计算机二级考试《JAVA》备考测试题「带答案」
  10. 旋转函数_【视频课】:一次函数拓展应用(图象的平移、旋转、轴对称及5种解题方法)...