Python - WebDriver 識別登錄驗證碼


Python - WebDriver 識別登錄驗證碼

沒什么可說的直接上代碼!

#-*-coding:utf-8-*-
# Time:2017/9/29 7:16
# Author:YangYangJun
import time
from pytesseract import *

from selenium import webdriver

from PIL import Image, ImageEnhance

import baseinfo

url = baseinfo.url

driver = webdriver.Firefox()

driver.maximize_window()

driver.get(url)
time.sleep(3)
driver.save_screenshot('verifyCode.png')  #截取當前網頁,該網頁有我們需要的驗證碼
time.sleep(2)
#定位驗證碼
imgelement = driver.find_element_by_xpath(".//*[@id='imgObj']")
#獲取驗證碼x,y軸坐標
location = imgelement.location
#獲取驗證碼的長寬
size = imgelement.size
driver.quit()
#寫成我們需要截取的位置坐標
rangle = (int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height']))
# 打開截圖
i = Image.open('verifyCode.png')
#使用Image的crop函數,從截圖中再次截取我們需要的區域
imgry=i.crop(rangle)  #使用Image的crop函數,從截圖中再次截取我們需要的區域

imgry.save('getVerifyCode.png')

im=Image.open('getVerifyCode.png')

im = im.convert('L')#圖像加強,二值化

sharpness =ImageEnhance.Contrast(im)#對比度增強

sharp_img = sharpness.enhance(2.0)

sharp_img.save("newVerifyCode.png")

newVerify = Image.open('newVerifyCode.png')

# 使用image_to_string識別驗證碼
text=image_to_string(newVerify).strip() #使用image_to_string識別驗證碼
#text1 = image_to_string('newVerifyCode.png').strip()
print text
#print text1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM