網站首頁 編程語言 正文
前言
在使用自動化登錄網站的時候,經常輸入用戶名和密碼后會遇到驗證碼。今天介紹一款通用驗證碼識別 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的三種等待方式(顯示等待,隱式等待,強制等待)可以參考其他博主的文章了解學習。
總結
對于現在已有的驗證碼圖片都有可能具備一定的識別能力。簡單來說,ddddocr 讓驗證碼識別變得如此簡單與易用,可以快速的檢測出圖片上的文字、數字或圖標,讓更多的伙伴能夠快速的破解網站的登錄驗證碼。
原文鏈接:https://blog.csdn.net/weixin_58839230/article/details/124243584
相關推薦
- 2022-12-21 Python?threading中lock的使用詳解_python
- 2022-11-24 Flask中Cookie和Session理解與作用介紹_python
- 2022-02-26 C#正則表達式Regex類的用法_C#教程
- 2022-05-24 調用無文檔說明的?Web?API過程描述_相關技巧
- 2022-10-19 React?Hook實現對話框組件_React
- 2023-05-29 React優雅的封裝SvgIcon組件示例_React
- 2022-09-06 C語言單鏈表遍歷與求和示例解讀_C 語言
- 2022-08-15 python中sort()和sorted()的區別及用法實例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支