會用到的庫的
1、selenium的webdriver
2、tesserocr或者pytesseract進行圖像識別
3、pillow的image進行圖片處理
1
2
3
|
from selenium import webdriver import tesserocr from pil import image |
獲取驗證碼圖片方法1:
1
2
3
4
5
6
7
8
9
10
11
|
def get_code_image(file_name): driver.save_screenshot(file_name) # 截取整個屏幕并保存 code_element = driver.find_element_by_class_name( "verify_code_img___1mei_" ) # 定位到驗證碼元素 left = code_element.location[ 'x' ] # 定位到截圖位置 top = code_element.location[ 'y' ] right = code_element.size[ 'width' ] + left bottom = code_element.size[ 'height' ] + top im = image. open (file_name) # 從文件讀取截圖,截取驗證碼位置再次保存 img = im.crop((left, top, right, bottom)) img.save(file_name) return file_name |
獲取驗證碼圖片方法2:
1
2
3
|
def get_code_image(file_name): code_element = driver.find_element_by_class_name( "verify_code_img___1mei_" ) # 定位到驗證碼元素 code_element.screenshot(file_name) |
注:此方法截圖時屏幕會閃動,可能引發bug,如下圖,目前沒有解決
處理驗證碼圖片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
def deal_code_image(file_name): image = image. open (file_name) # image.show() #查看處理前的圖片 # 處理圖片去除干擾 # 將圖片轉化為灰度圖像 image = image.convert( 'l' ) threshold = 90 # 設置臨界值,臨界值可調試 table = [] for i in range ( 256 ): if i < threshold: table.append( 0 ) else : table.append( 1 ) image = image.point(table, '1' ) # image.show() #查看處理后的圖片 # 1:使用tesseract庫識別圖片中的驗證碼 # res = tesserocr.image_to_text(image) # 2:使用pytesseract庫識別圖片中的驗證碼 res = pytesseract.image_to_string(image) # print(res) #查看識別出來的文案 res = res.replace( " " , "") #去除結果中的空格 return res |
處理前的圖片,有干擾,無法識別
處理后的圖片,基本可以識別
識別結果不一定準確,如果驗證碼輸入錯誤,可以點擊換一張圖片再次識別,多次嘗試,本次不做說明
到此這篇關于python3定位并識別圖片驗證碼實現自動登錄的文章就介紹到這了,更多相關python識別圖片驗證碼實現自動登錄內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/zloveyll/article/details/113246855