当前位置: 移动技术网 > IT编程>脚本编程>Python > Python爬虫入门教程 55-100 python爬虫高级技术之验证码篇

Python爬虫入门教程 55-100 python爬虫高级技术之验证码篇

2019年04月03日  | 移动技术网IT编程  | 我要评论

汽车音乐网,疯狂农场5中文版,蚯蚓图片

验证码探究

如果你是一个数据挖掘爱好者,那么验证码是你避免不过去的一个天坑,和各种验证码斗争,必然是你成长的一条道路,接下来的几篇文章,我会尽量的找到各种验证码,并且去尝试解决掉它,中间有些技术甚至我都没有见过,来吧,一起coding吧

数字+字母的验证码

我随便在百度图片搜索了一个验证码,如下
验证码
今天要做的是验证码识别中最简单的一种办法,采用pytesseract解决,它属于python当中比较简单的ocr识别库

库的安装

使用pytesseract之前,你需要通过pip 安装一下对应的模块 ,需要两个

pytesseract库还有图像处理的pillow库了

pip install pytesseract
pip install pillow

如果你安装了这两个库之后,编写一个识别代码,一般情况下会报下面这个错误

pytesseract.pytesseract.tesseractnotfounderror: tesseract is not installed or it's not in your path

这是由于你还缺少一部分内容

安装一个tesseract-ocr软件。这个软件是由google维护的开源的ocr软件。

下载地址 >

中文包的下载地址 >

选择你需要的版本进行下载即可

pillow库的基本操作

命令 释义
open() 打开一个图片
from pil import image
im = image.open("1.png")
im.show()
save() 保存文件
convert() convert() 是图像实例对象的一个方法,接受一个 mode 参数,用以指定一种色彩模式,mode 的取值可以是如下几种:
· 1 (1-bit pixels, black and white, stored with one pixel per byte)
· l (8-bit pixels, black and white)
· p (8-bit pixels, mapped to any other mode using a colour palette)
· rgb (3x8-bit pixels, true colour)
· rgba (4x8-bit pixels, true colour with transparency mask)
· cmyk (4x8-bit pixels, colour separation)
· ycbcr (3x8-bit pixels, colour video format)
· i (32-bit signed integer pixels)
· f (32-bit floating point pixels)

filter

from pil import image, imagefilter 
im = image.open(‘1.png’) 
# 高斯模糊 
im.filter(imagefilter.gaussianblur) 
# 普通模糊 
im.filter(imagefilter.blur) 
# 边缘增强 
im.filter(imagefilter.edge_enhance) 
# 找到边缘 
im.filter(imagefilter.find_edges) 
# 浮雕 
im.filter(imagefilter.emboss) 
# 轮廓 
im.filter(imagefilter.contour) 
# 锐化 
im.filter(imagefilter.sharpen) 
# 平滑 
im.filter(imagefilter.smooth) 
# 细节 
im.filter(imagefilter.detail)

format

format属性定义了图像的格式,如果图像不是从文件打开的,那么该属性值为none;
size属性是一个tuple,表示图像的宽和高(单位为像素);
mode属性为表示图像的模式,常用的模式为:l为灰度图,rgb为真彩色,cmyk为pre-press图像。如果文件不能打开,则抛出ioerror异常。

这个地方可以参照一篇博客,写的不错 >

验证码识别

注意安装完毕,如果还是报错,请找到模块 pytesseract.py 这个文件,对这个文件进行编辑

一般这个文件在 c:\program files\python36\lib\site-packages\pytesseract\pytesseract.py 位置

文件中 tesseract_cmd = 'tesseract' 改为自己的地址
例如: tesseract_cmd = 'c:\program files (x86)\tesseract-ocr\tesseract.exe' 

如果报下面的bug,请注意

error opening data file \program files (x86)\tesseract-ocr\tessdata/chi_sim.traineddata please make sure the tessdata_prefix environment variable

解决办法也比较容易,按照它的提示,表示缺失了 tessdata_prefix 这个环境变量。你只需要在系统环境变量中添加一条即可

将 tessdata_prefix=c:\program files (x86)\tesseract-ocr 添加环境变量

重启ide或者重新cmd,然后继续运行代码,这个地方注意需要用管理员运行你的py脚本

步骤分为

  1. 打开图片 image.open()
  2. pytesseract识别图片
import pytesseract
from pil import image

def main():
    image = image.open("1.jpg")
 
    text = pytesseract.image_to_string(image,lang="chi_sim")
    print(text)

if __name__ == '__main__':
    main()

测试英文,数字什么的基本没有问题,中文简直惨不忍睹。空白比较大的可以识别出来。唉~不好用
当然刚才那个7364 十分轻松的就识别出来了。

带干扰的验证码识别

接下来识别如下的验证码,我们首先依旧先尝试一下。运行代码发现没有任何显示。接下来需要对这个图片进行处理
在这里插入图片描述
基本原理都是完全一样的

  1. 彩色转灰度
  2. 灰度转二值
  3. 二值图像识别

彩色转灰度

im = im.convert('l')  

灰度转二值,解决方案比较成套路,采用阈值分割法,threshold为分割点

def inittable(threshold=140):
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    return table

调用

binaryimage = im.point(inittable(), '1')
binaryimage.show()

调整之后
python验证码
我们还需要对干扰线进行处理。在往下研究去,是图片深入处理的任务,对付小网站的简单验证码,这个办法足够了,本篇博文over,下一篇我们继续研究验证码。

参考链接

tesserocr github:
tesserocr pypi:
pytesserocr github:
pytesserocr pypi:
tesseract下载地址:
tesseract github:
tesseract 语言包:
tesseract文档:https://github.com/tesseract-ocr/tesseract/wiki/documentation

扫码关注微信公众账号,领取2t学习资源

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网