python使用Tesseract库识别验证
作者:凯耐 时间:2023-05-06 12:31:51
一、Tesseract简介
Tesseract是一个OCR库(OCR是英文Optical Character Recognition的缩写),它用来对文本资料进行扫描,然后对图像文件进行分析处理,获取文字及版面信息的过程,Tesseract是目前公认最优秀,识别相对精准的OCR库。
二、Tesseract的使用
1.下载并安装Tesseract:点击下载
2.在Windows系统下设置环境变量:
#根据下载安装文件的路径配置环境变量
set TESSDATA_PREFIX F:\Tesseract-OCR\
3.安装pytesseract模块
pip install pytesseract
4.在Python脚本中引入tesseract.exe应用程序的方式:
pytesseract.pytesseract.tesseract_cmd = r'F:\Tesseract-OCR\tesseract.exe'
5.案例演示
识别以下图片文字:
import pytesseract
from PIL import Image
#1.引入Tesseract程序
pytesseract.pytesseract.tesseract_cmd = r'F:\Tesseract-OCR\tesseract.exe'
#2.使用Image模块下的Open()函数打开图片
image = Image.open('6.jpg',mode='r')
print(image)
#3.识别图片文字
code= pytesseract.image_to_string(image)
print(code)
结果演示:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=611x210 at 0x1A5DFDCB4A8>
Google
注:tesseract-OCR引擎识别验证码有些无法识别,比如像豆瓣生成的验证码无法识别其内容,如果需要爬取豆瓣中的数据这时候就需要手动的输入验证码:
三、模拟登陆知乎源码
import requests
import time
import pytesseract
from PIL import Image
from bs4 import BeautifulSoup
def captcha(data):
with open('captcha.jpg','wb') as fp:
fp.write(data)
time.sleep(1)
image = Image.open("captcha.jpg")
text = pytesseract.image_to_string(image)
print "机器识别后的验证码为:" + text
command = raw_input("请输入Y表示同意使用,按其他键自行重新输入:")
if (command == "Y" or command == "y"):
return text
else:
return raw_input('输入验证码:')
def zhihuLogin(username,password):
# 构建一个保存Cookie值的session对象
sessiona = requests.Session()
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'}
# 先获取页面信息,找到需要POST的数据(并且已记录当前页面的Cookie)
html = sessiona.get('https://www.zhihu.com/#signin', headers=headers).content
# 找到 name 属性值为 _xsrf 的input标签,取出value里的值
_xsrf = BeautifulSoup(html ,'lxml').find('input', attrs={'name':'_xsrf'}).get('value')
# 取出验证码,r后面的值是Unix时间戳,time.time()
captcha_url = 'https://www.zhihu.com/captcha.gif?r=%d&type=login' % (time.time() * 1000)
response = sessiona.get(captcha_url, headers = headers)
data = {
"_xsrf":_xsrf,
"email":username,
"password":password,
"remember_me":True,
"captcha": captcha(response.content)
}
response = sessiona.post('https://www.zhihu.com/login/email', data = data, headers=headers)
print response.text
response = sessiona.get('https://www.zhihu.com/people/maozhaojun/activities', headers=headers)
print response.text
if __name__ == "__main__":
#username = raw_input("username")
#password = raw_input("password")
zhihuLogin('xxxx@qq.com','ALAxxxxIME')
来源:http://blog.csdn.net/weixin_36279318/article/details/79586022
标签:python,Tesseract,识别验证
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pandas数据查询的集中实现方法
2021-08-17 10:32:27
![](https://img.aspxhome.com/file/2023/3/104833_0s.png)
MySQL中Select查询语句的高级用法分享
2024-01-18 11:33:33
vue+vux实现移动端文件上传样式
2024-05-02 16:34:40
![](https://img.aspxhome.com/file/2023/2/130232_0s.png)
Python实现的拟合二元一次函数功能示例【基于scipy模块】
2023-10-09 00:32:02
![](https://img.aspxhome.com/file/2023/3/128323_0s.png)
PHP动态生成javascript文件的2个例子
2024-05-11 09:25:44
使用python进行图片的文字识别详细代码
2021-06-27 07:01:06
![](https://img.aspxhome.com/file/2023/6/101336_0s.jpg)
解决pytorch-gpu 安装失败的记录
2023-04-04 05:44:44
编译asp应用程序成为exe文件
2008-10-23 14:01:00
TensorFlow损失函数专题详解
2023-08-17 10:12:13
![](https://img.aspxhome.com/file/2023/3/85303_0s.gif)
python中opencv 直方图处理
2021-12-24 09:45:17
![](https://img.aspxhome.com/file/2023/5/90595_0s.webp)
对python中的argv和argc使用详解
2021-12-09 02:37:52
记一次python 爬虫爬取深圳租房信息的过程及遇到的问题
2022-03-22 02:09:02
![](https://img.aspxhome.com/file/2023/7/109657_0s.png)
AngularJS框架的ng-app指令与自动加载实现方法分析
2024-05-02 17:40:17
Python grequests模块使用场景及代码实例
2021-08-05 18:30:41
![](https://img.aspxhome.com/file/2023/1/124961_0s.png)
类型转换的小乐趣
2010-11-30 21:37:00
Python PIL读取的图像发生自动旋转的实现方法
2022-05-01 20:29:26
解决Jupyter无法导入已安装的 module问题
2022-05-13 07:14:18
![](https://img.aspxhome.com/file/2023/1/87561_0s.jpg)
Vue3.0 自己实现放大镜效果案例讲解
2024-04-09 10:45:54
![](https://img.aspxhome.com/file/2023/2/139852_0s.png)
python print出共轭复数的方法详解
2021-11-15 20:37:57
python从入门到精通(DAY 3)
2023-11-03 08:23:18
![](https://img.aspxhome.com/file/2023/8/63598_0s.png)