Python自动化——识别验证码
2017-12-20 本文已影响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里面。
![](https://img.haomeiwen.com/i6635063/4085528954363a8e.png)
2.2 验证tesserat环境变量配置是否成功
打开CMD,输入tesseract -v。出现版本信息,说明环境变量配置好了
![](https://img.haomeiwen.com/i6635063/8fc3873fce9ac55f.png)
3.pytessract安装
直接使用pip install pytesseract安装即可,或者使用easy_install pytesseract
用来连接tessearct进行验证码识别
4.简单的验证码识别例子
![](https://img.haomeiwen.com/i6635063/bb1536048e57c24c.png)
from pytesseract import image_to_string
from PIL import Image
img = Image.open(r"GetValidateCode.jpg")
# print(img)
print(image_to_string(img))
![](https://img.haomeiwen.com/i6635063/f4f379b73b33f492.png)
5.踩坑无数的小白
ok,这样就完成了一个简单的验证码识别。快去尝试一下吧。
当然过程可能不会这么的一帆风顺,可能会出现下面的错误。不要慌
![](https://img.haomeiwen.com/i6635063/ec78a6afd0cddb56.png)
让小白来带你一起踩坑。
首先找到你安装在Python目录下(我这里是Python34)的pytesseract目录里面的pytesseract.py文件
![](https://img.haomeiwen.com/i6635063/9d1e663b9d187984.png)
然后打开它。修改下面的路径(我这里用的Notepad++)
![](https://img.haomeiwen.com/i6635063/229c8e36b97ab311.png)
![](https://img.haomeiwen.com/i6635063/c978d030fee0ecb9.png)