美文网首页seleniumpython热爱者
Python自动化——识别验证码

Python自动化——识别验证码

作者: 努力学习的小白 | 来源:发表于2017-12-20 13:04 被阅读123次

    Python识别验证码主要依靠以下三个库来实现的。

    1.Pillow 安装

    Python3以后已经不支持PIL,所以要是用pillow,使用方法跟PIL一样。
    安装:pip install pillow

    2.安装tesseract-ocr
    下载地址:https://github.com/tesseract-ocr/tesseract/wiki/4.0-with-LSTM#400-alpha-for-windows

    Tesseract:是一个开源的光学字符识别(OCR)引擎,可在Apache 2.0许可下使用。它可以直接使用,或者(对于程序员)使用API从图像中提取打印的、手写的或打印的文本。它支持多种语言。
    Tesseract没有内置的GUI,但是在3rdParty页面上有几个可用的GUI。

    环境变量配置:
    2.1 将安装目录配置到环境变量和用户变量的path里面。


    环境变量配置

    2.2 验证tesserat环境变量配置是否成功
    打开CMD,输入tesseract -v。出现版本信息,说明环境变量配置好了

    环境验证
    3.pytessract安装

    直接使用pip install pytesseract安装即可,或者使用easy_install pytesseract
    用来连接tessearct进行验证码识别

    4.简单的验证码识别例子
    验证码
    from pytesseract import image_to_string
    from PIL import Image
    img = Image.open(r"GetValidateCode.jpg")
    # print(img)
    print(image_to_string(img))
    
    console输出
    5.踩坑无数的小白
    ok,这样就完成了一个简单的验证码识别。快去尝试一下吧。
    当然过程可能不会这么的一帆风顺,可能会出现下面的错误。不要慌
    image.png
    让小白来带你一起踩坑。
    首先找到你安装在Python目录下(我这里是Python34)的pytesseract目录里面的pytesseract.py文件
    pytesseract
    然后打开它。修改下面的路径(我这里用的Notepad++)
    修改路径 驱动路径
    到此为止,坑就成功的踩过去了。保存一下,就可以进行验证码识别了。识别率还是挺高的哈,
    最后搭配着截取验证码就可以愉快的实现了自动化识别验证码了。哈哈哈

    相关文章

      网友评论

      • __周__:root@kali:~/桌面# python yanzhengma.py
        Traceback (most recent call last):
        File "yanzhengma.py", line 5, in <module>
        print(image_to_string(img))
        File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 193, in image_to_string
        return run_and_get_output(image, 'txt', lang, config, nice)
        File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 140, in run_and_get_output
        run_tesseract(**kwargs)
        File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 111, in run_tesseract
        proc = subprocess.Popen(command, stderr=subprocess.PIPE)
        File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
        errread, errwrite)
        File "/usr/lib/python2.7/subprocess.py", line 1025, in _execute_child
        raise child_exception
        OSError: [Errno 2] No such file or directory

        大佬出现这种问题是什么情况?
        努力学习的小白:@无敌的小周 应该跟Python的版本和操作系统有关。
        __周__:@努力学习的小白 我已经把图片和脚本放到一个目录下了,按理说不应该,难道要制定工作路径?不能吧?我同学情况跟我也是一样
        努力学习的小白:@无敌的小周 找不到文件,你检查一下文件路径

      本文标题:Python自动化——识别验证码

      本文链接:https://www.haomeiwen.com/subject/wbcgwxtx.html