標籤:author from coding int open 二值化 映像 string sha
Python - WebDriver 識別登入驗證碼
沒什麼可說的直接上代碼!
#-*-coding:utf-8-*-# Time:2017/9/29 7:16# Author:YangYangJunimport timefrom pytesseract import *from selenium import webdriverfrom PIL import Image, ImageEnhanceimport baseinfourl = baseinfo.urldriver = 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.sizedriver.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
Python - WebDriver 識別登入驗證碼