您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python怎么通用驗(yàn)證碼識(shí)別OCR庫ddddocr”,在日常操作中,相信很多人在Python怎么通用驗(yàn)證碼識(shí)別OCR庫ddddocr問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Python怎么通用驗(yàn)證碼識(shí)別OCR庫ddddocr”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
通過命令將自動(dòng)安裝符合自己電腦環(huán)境的最新 ddddocr。
pip install ddddocr
如果安裝速度慢,可以連接國(guó)內(nèi)鏡像進(jìn)行安裝,命令如下:
pip install ddddocr -i https://pypi.tuna.tsinghua.edu.cn/simple/
import ddddocr ocr = ddddocr.DdddOcr() with open('code.png', 'rb') as f: img_bytes = f.read() res = ocr.classification(img_bytes) print('識(shí)別出的驗(yàn)證碼為:' + res)
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) # 獲取驗(yàn)證碼信息 def getVerification(self): # 獲取當(dāng)前文件的位置、并獲取保存截屏的位置 current_location = os.path.dirname(__file__) screenshot_path = os.path.join(current_location, "..", "VerificationCode") # 截取當(dāng)前網(wǎng)頁并放到自定義目錄下,并命名為printscreen,該截圖中有我們需要的驗(yàn)證碼 sleep(1) self.driver.save_screenshot(screenshot_path + '//' + 'printscreen.png') sleep(1) # 定位驗(yàn)證碼 imgelement = self.driver.find_element(By.XPATH, '驗(yàn)證碼圖片的Xpath定位') # 獲取驗(yàn)證碼x,y軸坐標(biāo) location = imgelement.location # 獲取驗(yàn)證碼的長(zhǎng)寬 size = imgelement.size # 寫成我們需要截取的位置坐標(biāo) 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函數(shù),從截圖中再次截取我們需要的區(qū)域 fimg = i.crop(rangle) fimg = fimg.convert('RGB') # 保存我們截下來的驗(yàn)證碼圖片,并讀取驗(yàn)證碼內(nèi)容 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('識(shí)別出的驗(yàn)證碼為:' + self.res) # 判斷驗(yàn)證碼錯(cuò)誤時(shí)的提示信息是否存在 def isElementPresent(self, by, value): try: element = self.driver.find_element(by=by, value=value) except NoSuchElementException: pass # 發(fā)生了NoSuchElementException異常,說明頁面中未找到該元素,返回False return False else: # 沒有發(fā)生異常,表示在頁面中找到了該元素,返回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, '驗(yàn)證碼輸入框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, '驗(yàn)證碼錯(cuò)誤時(shí)的提示信息Xpath定位') if isPresent is True: codeText = self.driver.find_element(By.XPATH, '驗(yàn)證碼錯(cuò)誤時(shí)的提示信息Xpath定位').text if codeText == "驗(yàn)證碼不正確": self.getVerification() sleep(2) self.driver.find_element(By.XPATH, '驗(yàn)證碼輸入框Xpath定位').clear() sleep(1) self.driver.find_element(By.XPATH, '驗(yàn)證碼輸入框Xpath定位').send_keys(self.res) sleep(1) self.driver.find_element(By.XPATH, '登錄按鈕Xpath定位').click() sleep(2) tips = self.driver.find_element(By.XPATH, '未輸入驗(yàn)證碼時(shí)的提示信息Xpath定位').text if tips == "請(qǐng)輸入驗(yàn)證碼": self.getVerification() sleep(2) self.driver.find_element(By.XPATH, '驗(yàn)證碼輸入框Xpath定位').click() sleep(1) self.driver.find_element(By.XPATH, '驗(yàn)證碼輸入框Xpath定位').send_keys(self.res) sleep(1) self.driver.find_element(By.XPATH, '登錄按鈕Xpath定位').click() sleep(2) continue else: print("驗(yàn)證碼正確,登錄成功!") except NoSuchElementException: pass else: isFlag = False sleep(5) self.driver.quit() if __name__ == '__main__': GetVerificationCode().login()
可以實(shí)現(xiàn):驗(yàn)證碼識(shí)別錯(cuò)誤后,繼續(xù)識(shí)別
到此,關(guān)于“Python怎么通用驗(yàn)證碼識(shí)別OCR庫ddddocr”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。