上一篇介紹了使用python模擬登陸網站,但是登陸的網站都是直接輸入賬號及密碼進行登陸,現在很多網站為了加強用戶安全性和提高反爬蟲機制都會有包括字符、圖片、手機驗證等等各式各樣的驗證碼。圖片驗證碼就是其中一種,而且識別難度越來越大,人為都比較難識別。本篇我們簡單介紹一下使用python登陸帶弱圖片驗證碼的網站。
圖片驗證碼
一般都通過加干擾線、粘連或扭曲等方式來增加強度。
登陸
我們選擇一個政務網站(圖片驗證碼的強度較低)。
點擊個人用戶登錄
訪問網站首頁以后我們發現需要先點擊個人用戶登陸
,且元素沒有name、id登標識不好獲取,所以我們直接執行里面的onClick方法
# 新建selenium瀏覽器對象,后面是geckodriver.exe下載后本地路徑
browser = webdriver.Firefox()
url = 'http://xxx.gov.cn/'
# 瀏覽器訪問登錄頁面
browser.get(url)
# 等待3s用於加載腳本文件
browser.implicitly_wait(3)
# 點擊個人登陸
browser.execute_script('showpersonlogin();')
獲取圖片驗證碼
我們可以通過save_screenshot
截圖,然后找到驗證碼元素,獲取元素位置然后在截圖的基礎上裁剪出驗證碼。
# 找到圖片驗證碼元素
img = browser.find_element_by_id('imgCode')
location = img.location
size = img.size
left = location['x']
top = location['y']
right = left + size['width']
bottom = top + size['height']
# 按照驗證碼的長寬,切割驗證碼
image_obj = loginPage.crop((left, top, right, bottom))
image_obj.save('code.png')
識別並登陸
由於該網站的驗證碼比較簡單可以直接用pytesseract
模塊的image_to_string方法
orcCode = pytesseract.image_to_string('code.png')
# 輸入用戶名
username = browser.find_element_by_id('personaccount')
username.send_keys('賬號')
# 輸入密碼
password = browser.find_element_by_id('personpassword')
password.send_keys('密碼')
# 輸入驗證碼
code = browser.find_element_by_id('captcha1')
code.send_keys(orcCode)
# 執行登錄
browser.execute_script('personlogin();')
# 關閉瀏覽器
# browser.quit()
識別較復雜驗證碼算法
網上找的算法,先將圖片轉為灰度圖,然后進行二值化處理(將圖像上的像素點的灰度值設置為0或255。如灰度大於等於閾值的像素,用255表示。否則為0。),再去噪(8鄰域降噪,判斷8個鄰域的黑色數量個數)。
ocrImage.py:
import pytesseract
from PIL import Image
from collections import defaultdict
# 獲取圖片中像素點數量最多的像素
def get_threshold(image):
pixel_dict = defaultdict(int)
# 像素及該像素出現次數的字典
rows, cols = image.size
for i in range(rows):
for j in range(cols):
pixel = image.getpixel((i, j))
pixel_dict[pixel] += 1
count_max = max(pixel_dict.values()) # 獲取像素出現出多的次數
pixel_dict_reverse = {v: k for k, v in pixel_dict.items()}
threshold = pixel_dict_reverse[count_max] # 獲取出現次數最多的像素點
return threshold
# 按照閾值進行二值化處理
# threshold: 像素閾值
def get_bin_table(threshold):
# 獲取灰度轉二值的映射table
table = []
for i in range(256):
rate = 0.1 # 在threshold的適當范圍內進行處理
if threshold * (1 - rate) <= i <= threshold * (1 + rate):
table.append(1)
else:
table.append(0)
return table
# 去掉二值化處理后的圖片中的噪聲點
def cut_noise(image):
rows, cols = image.size # 圖片的寬度和高度
change_pos = [] # 記錄噪聲點位置
# 遍歷圖片中的每個點,除掉邊緣
for i in range(1, rows - 1):
for j in range(1, cols - 1):
# pixel_set用來記錄該店附近的黑色像素的數量
pixel_set = []
# 取該點的鄰域為以該點為中心的九宮格
for m in range(i - 1, i + 2):
for n in range(j - 1, j + 2):
if image.getpixel((m, n)) != 1: # 1為白色,0位黑色
pixel_set.append(image.getpixel((m, n)))
# 如果該位置的九宮內的黑色數量小於等於4,則判斷為噪聲
if len(pixel_set) <= 4:
change_pos.append((i, j))
# 對相應位置進行像素修改,將噪聲處的像素置為1(白色)
for pos in change_pos:
image.putpixel(pos, 1)
return image # 返回修改后的圖片
# 識別圖片中的數字加字母
# 傳入參數為圖片路徑,返回結果為:識別結果
def ocr_img(img_path):
image = Image.open(img_path) # 打開圖片文件
imgry = image.convert('L') # 轉化為灰度圖
# 獲取圖片中的出現次數最多的像素,即為該圖片的背景
max_pixel = get_threshold(imgry)
# 將圖片進行二值化處理
table = get_bin_table(threshold=max_pixel)
out = imgry.point(table, '1')
# 去掉圖片中的噪聲(孤立點)
out = cut_noise(out)
# 僅識別圖片中的數字
# text = pytesseract.image_to_string(out, config='digits')
# 識別圖片中的數字和字母
text = pytesseract.image_to_string(out)
# 去掉識別結果中的特殊字符
exclude_char_list = ' .:\\|\'\"?![],()~@#$%^&*_+-={};<>/¥'
text = ''.join([x for x in text if x not in exclude_char_list])
return text
ocrImage.ocr_img('data/0021.png')
其他
針對不同的圖片驗證碼用的方法不盡相同,cv2模塊也提供了很多圖片的處理方法可以用於識別圖片驗證碼。
-
如使用cv2的腐蝕和碰撞方法就可以對圖片進行簡單的處理。
-
干擾條件較多、識別難度大的則需要依靠機器學習來完成。