Python驗證碼識別方式(使用pytesseract庫)
python中使用pytesseract庫進(jìn)行ocr識別,需要安裝Tesseract-OCR,通過指定pytesseract.tesseract_cmd路徑,可以將esseract-OCR集成到pytho程序中,避免客戶端電腦的依賴。
1、安裝Tesseract-OCR
Tesseract是一個高度精確的開源OCR(光學(xué)字符識別)系統(tǒng),廣泛應(yīng)用于文本識別項目中。
下載地址:
- https://digi.bib.uni-mannheim.de/tesseract/
- 選擇最新的穩(wěn)定版下載
安裝程序:下載后安裝程序
中文包下載:
- 地址:https://gitcode.com/open-source-toolkit/90e2f
- 下載了最新版本的chi-sim.traineddata文件,復(fù)制到Tesseract的
tessdata目錄下 - 通常,路徑類似于C:\Program Files\tesseract\tessdata(Windows)
- 或 /usr/share/tesseract-ocr/4.00/tessdata(Linux)。
2、在python中使用
安裝依賴
pip install pytesseract
3、本地圖片識別
import pytesseract
from PIL import Image
# 獲取文件的絕對路徑
def get_abspath(filename):
try:
current_dir = os.getcwd()
filename = os.path.normpath(os.path.join(current_dir, filename))
# print(f"get_abspath文件路徑:{filename}")
return filename
except Exception as e:
print(f"獲取文件絕對路徑時出現(xiàn)錯誤: {e}")
return ""
# 手動指定路徑(Windows常見) Tesseract 系統(tǒng)路徑
driver_path = r"Tesseract-OCR\\tesseract.exe"
pytesseract.pytesseract.tesseract_cmd = get_abspath(driver_path)
#使用示例
if __name__ == "__main__":
# 1 識別本地圖片
# 英文識別
current_dir = os.getcwd()
filename = os.path.normpath(os.path.join(current_dir, f"code.jpg"))
file = Image.open(filename)
text = pytesseract.image_to_string(file, lang="eng")
print(text)
#中文識別,需要下載語言包
filename = os.path.normpath(os.path.join(current_dir, f"sushi.png"))
file = Image.open(filename)
text = pytesseract.image_to_string(file, lang='chi_sim')
print(f"識別結(jié)果:{text}")識別結(jié)果示例:

4、結(jié)合playwright動態(tài)識別網(wǎng)站驗證碼
import os
import pytesseract
from PIL import Image
from playwright.sync_api import Playwright
import tools.pwHander as pwHander
from PIL import Image
# 獲取文件的絕對路徑
def get_abspath(filename):
try:
current_dir = os.getcwd()
filename = os.path.normpath(os.path.join(current_dir, filename))
# print(f"get_abspath文件路徑:{filename}")
return filename
except Exception as e:
print(f"獲取文件絕對路徑時出現(xiàn)錯誤: {e}")
return ""
# 手動指定路徑(Windows常見) Tesseract 系統(tǒng)路徑
driver_path = r"Tesseract-OCR\\tesseract.exe"
pytesseract.pytesseract.tesseract_cmd = get_abspath(driver_path)
# 驗證碼圖片識別
def get_captcha(page: Playwright, element_selector="img#captcha", file_name="code.jpg"):
try:
current_dir = os.getcwd()
filename = os.path.normpath(os.path.join(current_dir, f"{file_name}"))
# 通過class選擇器獲取img元素
code_img = page.locator(element_selector)
if not code_img:
raise ValueError("驗證碼元素未找到!")
# 刷新驗證碼
# code_img.click()
# 下載驗證碼圖片
code_img.screenshot(path=filename)
file = Image.open(filename)
text = pytesseract.image_to_string(file, lang="eng")
print("驗證碼識別結(jié)果:", text)
return text.strip()
except Exception as e:
print(f"獲取驗證碼 失?。簕str(e)}")
return ""
#使用示例
if __name__ == "__main__":
# 2 動態(tài)識別網(wǎng)站驗證碼
with sync_playwright() as p:
browser = p.chromium.launch(headless=False, slow_mo=1000)
context = browser.new_context()
page = context.new_page()
page.goto("測試網(wǎng)址")
# 驗證碼圖片下載
imgText = get_captcha(page, "img#jcaptcha")
print(f"驗證碼:{imgTest}")
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)遍歷數(shù)據(jù)庫并獲取key的值
本文給大家分享的是Python實現(xiàn)遍歷數(shù)據(jù)庫并獲取key的值的方法,主要是使用for循環(huán)來實現(xiàn),有需要的小伙伴可以參考下。2015-05-05
Python實現(xiàn)用手機(jī)監(jiān)控遠(yuǎn)程控制電腦的方法
這篇文章主要介紹了Python實現(xiàn)用手機(jī)監(jiān)控遠(yuǎn)程控制電腦的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
基于Python利用Faker批量測試數(shù)據(jù)
這篇文章主要介紹了基于Python利用Faker批量測試數(shù)據(jù)。測試過程中,經(jīng)常需要批量去造數(shù)據(jù),方法有很多,最簡單方便的應(yīng)該是使用python?的一個三方庫Faker。下面我們就來看看三方庫Faker如何批量測試數(shù)據(jù),需要的朋友可以參考一下2022-03-03

