Python通用验证码识别OCR库ddddocr的安装使用教程
作者:PandaMan~ 时间:2021-11-04 21:21:27
前言
在使用自动化登录网站的时候,经常输入用户名和密码后会遇到验证码。今天介绍一款通用验证码识别 OCR库,对验证码识别彻底说拜拜,它的名字是 ddddocr(带带弟弟 OCR )。这里主要以字母数字类验证码进行说明。
项目地址:https://github.com/sml2h3/ddddocr
一、安装ddddocr
通过命令将自动安装符合自己电脑环境的最新 ddddocr。
pip install ddddocr
如果安装速度慢,可以连接国内镜像进行安装,命令如下:
pip install ddddocr -i https://pypi.tuna.tsinghua.edu.cn/simple/
二、使用ddddocr
1. 使用举例
import ddddocr
ocr = ddddocr.DdddOcr()
with open('code.png', 'rb') as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
print('识别出的验证码为:' + res)
2. 完整代码
import os
import ddddocr
from time import sleep
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
class GetVerificationCode:
def __init__(self):
self.res = None
url = '要登录的地址'
self.driver = webdriver.Chrome()
self.driver.maximize_window() # 将浏览器最大化
self.driver.get(url)
# 获取验证码信息
def getVerification(self):
# 获取当前文件的位置、并获取保存截屏的位置
current_location = os.path.dirname(__file__)
screenshot_path = os.path.join(current_location, "..", "VerificationCode")
# 截取当前网页并放到自定义目录下,并命名为printscreen,该截图中有我们需要的验证码
sleep(1)
self.driver.save_screenshot(screenshot_path + '//' + 'printscreen.png')
sleep(1)
# 定位验证码
imgelement = self.driver.find_element(By.XPATH, '验证码图片的Xpath定位')
# 获取验证码x,y轴坐标
location = imgelement.location
# 获取验证码的长宽
size = imgelement.size
# 写成我们需要截取的位置坐标
rangle = (int(location['x'] + 430),
int(location['y'] + 200),
int(location['x'] + size['width'] + 530),
int(location['y'] + size['height'] + 250))
# 打开截图
i = Image.open(screenshot_path + '//' + 'printscreen.png')
# 使用Image的crop函数,从截图中再次截取我们需要的区域
fimg = i.crop(rangle)
fimg = fimg.convert('RGB')
# 保存我们截下来的验证码图片,并读取验证码内容
fimg.save(screenshot_path + '//' + 'code.png')
ocr = ddddocr.DdddOcr()
with open(screenshot_path + '//' + 'code.png', 'rb') as f:
img_bytes = f.read()
self.res = ocr.classification(img_bytes)
print('识别出的验证码为:' + self.res)
# 判断验证码错误时的提示信息是否存在
def isElementPresent(self, by, value):
try:
element = self.driver.find_element(by=by, value=value)
except NoSuchElementException:
pass
# 发生了NoSuchElementException异常,说明页面中未找到该元素,返回False
return False
else:
# 没有发生异常,表示在页面中找到了该元素,返回True
return True
# 登录
def login(self):
self.getVerification()
self.driver.find_element(By.XPATH, '用户名输入框Xpath定位').send_keys('用户名')
self.driver.find_element(By.XPATH, '密码输入框Xpath定位').send_keys('密码')
self.driver.find_element(By.XPATH, '验证码输入框Xpath定位').send_keys(self.res)
sleep(1)
self.driver.find_element(By.XPATH, '登录按钮Xpath定位').click()
sleep(2)
isFlag = True
while isFlag:
try:
isPresent = self.isElementPresent(By.XPATH, '验证码错误时的提示信息Xpath定位')
if isPresent is True:
codeText = self.driver.find_element(By.XPATH, '验证码错误时的提示信息Xpath定位').text
if codeText == "验证码不正确":
self.getVerification()
sleep(2)
self.driver.find_element(By.XPATH, '验证码输入框Xpath定位').clear()
sleep(1)
self.driver.find_element(By.XPATH, '验证码输入框Xpath定位').send_keys(self.res)
sleep(1)
self.driver.find_element(By.XPATH, '登录按钮Xpath定位').click()
sleep(2)
tips = self.driver.find_element(By.XPATH,
'未输入验证码时的提示信息Xpath定位').text
if tips == "请输入验证码":
self.getVerification()
sleep(2)
self.driver.find_element(By.XPATH, '验证码输入框Xpath定位').click()
sleep(1)
self.driver.find_element(By.XPATH, '验证码输入框Xpath定位').send_keys(self.res)
sleep(1)
self.driver.find_element(By.XPATH, '登录按钮Xpath定位').click()
sleep(2)
continue
else:
print("验证码正确,登录成功!")
except NoSuchElementException:
pass
else:
isFlag = False
sleep(5)
self.driver.quit()
if __name__ == '__main__':
GetVerificationCode().login()
3. 验证码样例
4. 识别结果
可以实现:验证码识别错误后,继续识别
三、代码说明
本文代码中时间等待都是使用了强制等待,如有需要可对代码进行修改,可以使用显示等待。关于selenium的三种等待方式(显示等待,隐式等待,强制等待)可以参考其他博主的文章了解学习。
来源:https://blog.csdn.net/weixin_58839230/article/details/124243584
标签:验证码识别,ocr库,ddddocr
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp封装dll源码分享
2008-09-25 17:20:00
![](https://img.aspxhome.com/file/UploadPic/20089/25/asp-dll_50s.jpg)
Django模板导入母版继承和自定义返回Html片段过程解析
2023-06-16 21:13:38
Python过滤txt文件内重复内容的方法
2023-08-04 17:41:48
![](https://img.aspxhome.com/file/2023/4/73004_0s.jpg)
使用Python的Bottle框架写一个简单的服务接口的示例
2023-09-01 17:29:50
选项卡动态增删的效果(内嵌框架)
2008-05-22 12:59:00
![](https://img.aspxhome.com/file/UploadPic/20085/22/asgard_cardprogram_62s.jpg)
JavaScript性能优化小技巧,创建文档碎片
2010-03-31 18:27:00
一行Python代码制作动态二维码的实现
2023-11-04 02:32:19
![](https://img.aspxhome.com/file/2023/7/78847_0s.jpg)
PHP连接MSSQL方法汇总
2023-11-17 19:34:36
Oracle学习笔记(六)
2012-01-05 18:55:27
Python利用Matplotlib库实现绘制饼形图
2021-08-13 11:08:51
![](https://img.aspxhome.com/file/2023/2/95132_0s.png)
django-crontab 定时执行任务方法的实现
2021-04-12 06:22:36
python 图像平移和旋转的实例
2021-03-06 23:59:49
asp如何用WSH获取机器的IP配置信息?
2010-06-13 14:39:00
python爬虫urllib中的异常模块处理
2022-12-06 10:42:33
![](https://img.aspxhome.com/file/2023/9/87699_0s.png)
搜索结果页(SERP)之:不要被F型浏览忽悠了
2009-09-07 12:34:00
![](https://img.aspxhome.com/file/UploadPic/20099/7/jakob-nielsen-58s.jpg)
Python通过wordcloud库实现将单词生成词云
2022-02-24 20:17:18
![](https://img.aspxhome.com/file/2023/3/83843_0s.png)
详解python里的命名规范
2023-02-11 20:07:02
JS实现简易图片轮播效果的方法
2023-07-21 08:19:16
![](https://img.aspxhome.com/file/2023/9/55939_0s.png)
开源MySQL公司停止提供企业版源代码tar包
2009-01-14 13:02:00
VS 2010 Ultimate架构代码探索
2010-05-02 20:38:00
![](https://img.aspxhome.com/file/UploadPic/20105/2/201052204722882s.jpg)