安裝 tesseract 庫
利用tesseract 訓(xùn)練數(shù)據(jù)大猛。最終識別驗(yàn)證碼。
先識別簡單的驗(yàn)證碼 數(shù)據(jù)包用chi_sim诅需。 (復(fù)雜驗(yàn)證碼需要訓(xùn)練)
破解驗(yàn)證碼:
image3.jpeg
image6.jpeg
# -*- coding: utf-8 -*-
from selenium import webdriver
import pytesseract
from PIL import Image
import re
import redis
cookie_name = "m_"
username = "xxxxxx"
password = "xxxxxx"
login_url = "http://xxx/"
# 像素識別
def binarizing(img, threshold):
pixdata = img.load()
w, h = img.size
for y in range(h):
for x in range(w):
if pixdata[x, y] < threshold:
pixdata[x, y] = 0
else:
pixdata[x, y] = 255
return img
def getCode():
# open image
image = Image.open('a.png')
# image.show()
ic = image.convert("L")
i = binarizing(ic, 85)
# i.show()
code = pytesseract.image_to_string(i, lang='chi_sim')
nums = re.findall(r"\d+\.?\d*", code)
print(nums)
if len(nums) == 1:
result = int(nums[0])
elif len(nums) >= 2:
result = int(nums[0]) + int(nums[1])
else:
result = 0
return result
browser = webdriver.Firefox(executable_path="/Users/guoxingjun/Downloads/geckodriver")
browser.set_page_load_timeout(1)
browser.set_script_timeout(1)
browser.delete_all_cookies()
browser.maximize_window() # 瀏覽器最大化
try:
browser.get(login_url) # 該處為具體網(wǎng)址
except:
browser.close()
browser.implicitly_wait(1)
browser.get_screenshot_as_file('a.png')
location = browser.find_element_by_class_name('info_yanzhen').location
size = browser.find_element_by_class_name('info_yanzhen').size
left = location['x'] * 2
top = location['y'] * 2
right = location['x'] * 2 + size['width'] * 2 + 20
bottom = location['y'] * 2 + size['height'] * 2 + 20
a = Image.open("a.png")
im = a.crop((left, top, right, bottom))
im.save('a.png')
#
# 圖片轉(zhuǎn)換成字符
vcode = getCode()
print(vcode)
# 填充用戶名 密碼 驗(yàn)證碼
browser.find_element_by_name("username").send_keys(username)
browser.find_element_by_name("password").send_keys(password)
browser.find_element_by_name("confirmationCode").send_keys(vcode)
# 點(diǎn)擊登錄
browser.find_element_by_id("btnSubmit").click()
browser.implicitly_wait(2)
print(browser.find_element_by_id("btnSubmit").text)
cookie = {}
for i in browser.get_cookies():
cookie[cookie_name + i["name"]] = i["value"]
browser.implicitly_wait(1)
browser.quit()