Python驗(yàn)證碼識(shí)別方式(使用pytesseract庫(kù))
python中使用pytesseract庫(kù)進(jìn)行ocr識(shí)別,需要安裝Tesseract-OCR,通過(guò)指定pytesseract.tesseract_cmd路徑,可以將esseract-OCR集成到pytho程序中,避免客戶(hù)端電腦的依賴(lài)。
1、安裝Tesseract-OCR
Tesseract是一個(gè)高度精確的開(kāi)源OCR(光學(xué)字符識(shí)別)系統(tǒng),廣泛應(yīng)用于文本識(shí)別項(xiàng)目中。
下載地址:
- https://digi.bib.uni-mannheim.de/tesseract/
- 選擇最新的穩(wěn)定版下載
安裝程序:下載后安裝程序
中文包下載:
- 地址:https://gitcode.com/open-source-toolkit/90e2f
- 下載了最新版本的chi-sim.traineddata文件,復(fù)制到Tesseract的
tessdata
目錄下 - 通常,路徑類(lèi)似于C:\Program Files\tesseract\tessdata(Windows)
- 或 /usr/share/tesseract-ocr/4.00/tessdata(Linux)。
2、在python中使用
安裝依賴(lài)
pip install pytesseract
3、本地圖片識(shí)別
import pytesseract from PIL import Image # 獲取文件的絕對(duì)路徑 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"獲取文件絕對(duì)路徑時(shí)出現(xiàn)錯(cuò)誤: {e}") return "" # 手動(dòng)指定路徑(Windows常見(jiàn)) Tesseract 系統(tǒng)路徑 driver_path = r"Tesseract-OCR\\tesseract.exe" pytesseract.pytesseract.tesseract_cmd = get_abspath(driver_path) #使用示例 if __name__ == "__main__": # 1 識(shí)別本地圖片 # 英文識(shí)別 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) #中文識(shí)別,需要下載語(yǔ)言包 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"識(shí)別結(jié)果:{text}")
識(shí)別結(jié)果示例:
4、結(jié)合playwright動(dòng)態(tài)識(shí)別網(wǎng)站驗(yàn)證碼
import os import pytesseract from PIL import Image from playwright.sync_api import Playwright import tools.pwHander as pwHander from PIL import Image # 獲取文件的絕對(duì)路徑 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"獲取文件絕對(duì)路徑時(shí)出現(xiàn)錯(cuò)誤: {e}") return "" # 手動(dòng)指定路徑(Windows常見(jiàn)) Tesseract 系統(tǒng)路徑 driver_path = r"Tesseract-OCR\\tesseract.exe" pytesseract.pytesseract.tesseract_cmd = get_abspath(driver_path) # 驗(yàn)證碼圖片識(shí)別 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}")) # 通過(guò)class選擇器獲取img元素 code_img = page.locator(element_selector) if not code_img: raise ValueError("驗(yàn)證碼元素未找到!") # 刷新驗(yàn)證碼 # code_img.click() # 下載驗(yàn)證碼圖片 code_img.screenshot(path=filename) file = Image.open(filename) text = pytesseract.image_to_string(file, lang="eng") print("驗(yàn)證碼識(shí)別結(jié)果:", text) return text.strip() except Exception as e: print(f"獲取驗(yàn)證碼 失?。簕str(e)}") return "" #使用示例 if __name__ == "__main__": # 2 動(dòng)態(tài)識(shí)別網(wǎng)站驗(yàn)證碼 with sync_playwright() as p: browser = p.chromium.launch(headless=False, slow_mo=1000) context = browser.new_context() page = context.new_page() page.goto("測(cè)試網(wǎng)址") # 驗(yàn)證碼圖片下載 imgText = get_captcha(page, "img#jcaptcha") print(f"驗(yàn)證碼:{imgTest}")
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
用Python將GIF動(dòng)圖分解成多張靜態(tài)圖片
今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著如何用Python將GIF動(dòng)圖分解成多張靜態(tài)圖片展開(kāi),文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06對(duì)numpy數(shù)據(jù)寫(xiě)入文件的方法講解
今天小編就為大家分享一篇對(duì)numpy數(shù)據(jù)寫(xiě)入文件的方法講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Python實(shí)現(xiàn)遍歷數(shù)據(jù)庫(kù)并獲取key的值
本文給大家分享的是Python實(shí)現(xiàn)遍歷數(shù)據(jù)庫(kù)并獲取key的值的方法,主要是使用for循環(huán)來(lái)實(shí)現(xiàn),有需要的小伙伴可以參考下。2015-05-05pytorch?tensor內(nèi)所有元素相乘實(shí)例
這篇文章主要介紹了pytorch?tensor內(nèi)所有元素相乘實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Python實(shí)現(xiàn)用手機(jī)監(jiān)控遠(yuǎn)程控制電腦的方法
這篇文章主要介紹了Python實(shí)現(xiàn)用手機(jī)監(jiān)控遠(yuǎn)程控制電腦的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04python正則匹配查詢(xún)辦理進(jìn)度示例分享
分享原創(chuàng)的一段查詢(xún)通行證辦理進(jìn)度查詢(xún)的python 3.3代碼,利用socket請(qǐng)求相關(guān)網(wǎng)站,獲得結(jié)果后利用正則找出辦理進(jìn)度2013-12-12基于Python利用Faker批量測(cè)試數(shù)據(jù)
這篇文章主要介紹了基于Python利用Faker批量測(cè)試數(shù)據(jù)。測(cè)試過(guò)程中,經(jīng)常需要批量去造數(shù)據(jù),方法有很多,最簡(jiǎn)單方便的應(yīng)該是使用python?的一個(gè)三方庫(kù)Faker。下面我們就來(lái)看看三方庫(kù)Faker如何批量測(cè)試數(shù)據(jù),需要的朋友可以參考一下2022-03-03