Python驗證碼截取識別代碼實例
1.環(huán)境
python3.7 selenium webdriver PIL Image2.下面demo是截取“去哪兒”官網(wǎng)的驗證碼
# -*- coding=utf-8 -*-# CodeDemo.py# PyCharm Slade 2019/7/20# import selenium,osfrom selenium import webdriverfrom PIL import Imagedef aucthcode(coderddr): ''' 傳參驗證碼的Xpath 頁面全圖為’code.png’ 命名這個你們開心就好 驗證碼截圖名為'aucthcode.png :param coderddr: :return: ''' driver.save_screenshot(’code.png’) element = driver.find_element_by_xpath(coderddr) # 獲取驗證碼的div位置 left = element.location[’x’] +280 top = element.location[’y’] +81 right = left + element.size[’width’] + int(13) bottom = top + element.size[’height’] + int(2) img = Image.open(’code.png’) imgcod = img.crop((left,top,right,bottom)) # 根據(jù) div的長寬截圖 imgcod.save(’aucthcode.png’) print((left,top,right,bottom))if __name__ == ’__main__’: driver = webdriver.Chrome() driver.maximize_window() driver.get(’https://user.qunar.com/passport/login.jsp’) driver.find_element_by_css_selector(’a.pwd-login’).click() aucthcode(’//*[@id='captcha'][1]/p[1]’) driver.quit() # img = Image.open(’code.png’) # imgcod = img.crop((1392,393,1490,425)) # 根據(jù) div的長寬截圖 # imgcod.save(’aucthcodeN.png’)
注:代碼中的驗證碼截取坐標,不一定是標準的,我是通過獲取元素的坐標為基礎(chǔ)參數(shù),然后在main里面進行調(diào)試最終得到我適用的坐標
運行效果展示 code.png
運行截取的驗證碼效果展示 aucthcode.png
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. React+umi+typeScript創(chuàng)建項目的過程2. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析3. SharePoint Server 2019新特性介紹4. ASP中常用的22個FSO文件操作函數(shù)整理5. 三個不常見的 HTML5 實用新特性簡介6. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp7. .Net core 的熱插拔機制的深入探索及卸載問題求救指南8. 無線標記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁9. 讀大數(shù)據(jù)量的XML文件的讀取問題10. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯誤頁的問題
