Python - WebDriver 識別登入驗證碼

來源:互聯網
上載者:User

標籤: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 識別登入驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.