Python爬虫入门教程 55-100

发布时间:2019-05-23 21:23:13编辑:auto阅读(1893)

    验证码探究

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

    下载地址 > https://github.com/tesseract-ocr/tesseract/wiki

    中文包的下载地址 > https://github.com/tesseract-ocr/tessdata

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

    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异常。

    这个地方可以参照一篇博客,写的不错 > https://www.cnblogs.com/mapu/p/8341108.html

    验证码识别

    注意安装完毕,如果还是报错,请找到模块 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:https://github.com/sirfz/tesserocr
    tesserocr PyPI:https://pypi.python.org/pypi/tesserocr
    pytesserocr GitHub:https://github.com/madmaze/pytesseract
    pytesserocr PyPI:https://pypi.org/project/pytesseract/
    tesseract下载地址:http://digi.bib.uni-mannheim.de/tesseract
    tesseract GitHub:https://github.com/tesseract-ocr/tesseract
    tesseract 语言包:https://github.com/tesseract-ocr/tessdata
    tesseract文档:https://github.com/tesseract-ocr/tesseract/wiki/Documentation

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

关键字