Python完全識(shí)別驗(yàn)證碼自動(dòng)登錄實(shí)例詳解
1、直接貼代碼
#!C:/Python27 #coding=utf-8 from selenium import webdriver from selenium.webdriver.common.keys import Keys from pytesser import * from PIL import Image,ImageEnhance,ImageFilter from selenium.common.exceptions import NoSuchElementException,TimeoutException import os,time def before(): driver.get(src) time.sleep(1) driver.maximize_window() # 瀏覽器全屏顯示 print ('\n瀏覽器全屏顯示 ...') def Convertimg(): imglocation = ("http://*[@id='loginForm']/div[4]/div[2]/img[1]") #下載驗(yàn)證碼圖片保存到本地 driver.save_screenshot('E:\\pythonScript\\Codeimages\\code.png') #打開本地圖片 im = Image.open('E:\\pythonScript\\Codeimages\\code.png') left = driver.find_element_by_xpath(imglocation).location['x'] top = driver.find_element_by_xpath(imglocation).location['y'] right = driver.find_element_by_xpath(imglocation).location['x'] + driver.find_element_by_xpath(imglocation).size['width'] bottom = driver.find_element_by_xpath(imglocation).location['y'] + driver.find_element_by_xpath(imglocation).size['height'] im = im.crop((left, top, right, bottom)) im.save('E:\\pythonScript\\Codeimages\\screenshot.png') print u"\n保存驗(yàn)證碼圖片完成" #移除截屏的圖片 os.remove('E:\\pythonScript\\Codeimages\\code.png') print u"\n刪除截屏圖片完成" #處理驗(yàn)證碼圖片 src = ('E:\\pythonScript\\Codeimages\\screenshot.png') #調(diào)用裁剪圖片方法 Cutedge(src) #移除截屏的圖片 os.remove('E:\\pythonScript\\Codeimages\\screenshot.png') #灰化圖片處理 im = Image.open('E:\\pythonScript\\Codeimages\\CutedgeImage.png') imgry = im.convert('L') #二值化處理 threshold = 100 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) out = imgry.point(table, '1') out.save('E:\\pythonScript\\Codeimages\\rgb.png') #vcode = pytesseract.image_to_string(out) #print (vcode) txtcode = image_to_string(out) print u"\n識(shí)別出驗(yàn)證碼文字為:",image_to_string(out) print len(txtcode.strip()) print if len(txtcode.strip()) == 4: print u"長度相等" else: print u"長度不相等,退出" driver.quit() #輸入用戶名和密碼 driver.find_element_by_id("username").send_keys("123456") driver.find_element_by_id("password").send_keys("123456") time.sleep(2) #對(duì)文本框輸入驗(yàn)證碼值 driver.find_element_by_id("verifyCode").send_keys(txtcode.strip()) time.sleep(3) #點(diǎn)擊登錄 driver.find_element_by_xpath("http://*[@id='loginForm']/div[5]/div/img").click() #driver.find_element_by_class_name('loginbtn').click() time.sleep(7) #針對(duì)有黑色邊框的驗(yàn)證碼圖片的裁剪邊緣 def Cutedge(src): #設(shè)置要裁剪的區(qū)域 im = Image.open(src) w, h = im.size print u"\n驗(yàn)證碼原圖寬、高尺寸為:",w,h box = (2,2,110,30) im.crop(box).save('E:\\pythonScript\\Codeimages\\CutedgeImage.png') print u"\n保存裁剪的圖片 CutedgeImage.png" # src = ("https://www.test.com") driver = webdriver.Chrome() #Firefox()# def method_2(src): before() #調(diào)用圖片裁剪方法 Convertimg() def clickInput(): driver.find_element_by_id("inputButton").click() print "\nInput Click Finish" def clickOutput(): print u"\n開始執(zhí)行點(diǎn)擊事件" #開始執(zhí)行點(diǎn)擊事件 inputButton driver.find_element_by_id("outputButton").click() time.sleep(2) print (u'\n開始執(zhí)行任務(wù),執(zhí)行間隔時(shí)間為10分鐘 ...') for i in range(1,4): ISOTIMEFORMAT="%Y-%m-%d %X" strTime = time.strftime( ISOTIMEFORMAT, time.localtime()) driver.refresh() print u"\n正在執(zhí)行第 ",i,"次...",strTime time.sleep(5) driver.find_element_by_id("outputButton").click() time.sleep(30) print #刷新瀏覽器 print u"\n刷新當(dāng)前頁面 ..." driver.refresh() print (u'\n等待間隔時(shí)間為9分鐘 ...') time.sleep(505) print u"\n已執(zhí)行完第 ",i,u"次,",u"已等待",i*10,u"分鐘" print (u'\n已執(zhí)行完成...At The End OF,'+strTime) driver.quit() def isPass(): try: #driver.find_element_by_id("username").is_displayed() == True driver.find_element_by_id('status').text == (u"驗(yàn)證碼不正確!") print (u"\n****校驗(yàn)提示信息_驗(yàn)證碼輸入不正確****") driver.quit() print (u"\n關(guān)閉瀏覽器,執(zhí)行外層循環(huán)...") except Exception: print (u"\n****校驗(yàn)提示信息_驗(yàn)證碼輸入正確****") clickOutput() #------ click Output method_2(src) #進(jìn)入工作頁面 isPass() #clickInput() #------ click Input #clickOutput() #------ click Output for i in range(1,6): driver = webdriver.Chrome() src = ("https://www.test.com") method_2(src) isPass() #clickOutput()
2、控制臺(tái)日志
瀏覽器全屏顯示 ... 獲取到元素的文本值為: 保存驗(yàn)證碼圖片完成 刪除截屏圖片完成 驗(yàn)證碼原圖寬、高尺寸為: 113 34 保存裁剪的圖片 CutedgeImage.png 識(shí)別出驗(yàn)證碼文字為: gnbn 開始執(zhí)行任務(wù),執(zhí)行間隔時(shí)間為10分鐘 ... 正在執(zhí)行第 1 次... 2017-05-25 18:10:24 刷新當(dāng)前頁面 ... 等待間隔時(shí)間為9分鐘 ...
以上這篇Python完全識(shí)別驗(yàn)證碼自動(dòng)登錄實(shí)例詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python的格式化輸出(format,%)實(shí)例詳解
Python中格式化字符串目前有兩種陣營:%和format,哪一種比較適合我們使用呢?下面腳本之家小編給大家介紹下python的格式化輸出(format,%)實(shí)例詳解,感興趣的朋友一起看看吧2018-06-06使用Python請(qǐng)求http/https時(shí)如何設(shè)置失敗重試次數(shù)
這篇文章主要介紹了使用Python請(qǐng)求http/https時(shí)如何設(shè)置失敗重試次數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06Python實(shí)現(xiàn)將MySQL數(shù)據(jù)庫表中的數(shù)據(jù)導(dǎo)出生成csv格式文件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將MySQL數(shù)據(jù)庫表中的數(shù)據(jù)導(dǎo)出生成csv格式文件的方法,涉及Python針對(duì)mysql數(shù)據(jù)庫的連接、查詢、csv格式數(shù)據(jù)文件的生成等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Python?pycharm讀取文件相對(duì)路徑與絕對(duì)路徑的方法
這篇文章主要給大家介紹了關(guān)于Python?pycharm讀取文件相對(duì)路徑與絕對(duì)路徑的方法,絕對(duì)路徑就是文件的真正存在的路徑,是指從硬盤的根目錄(盤符)開始,進(jìn)行一級(jí)級(jí)目錄指向文件,相對(duì)路徑就是以當(dāng)前文件為基準(zhǔn)進(jìn)行一級(jí)級(jí)目錄指向被引用的資源文件,需要的朋友可以參考下2023-12-12以Python的Pyspider為例剖析搜索引擎的網(wǎng)絡(luò)爬蟲實(shí)現(xiàn)方法
這篇文章主要介紹了以Python的Pyspider為例剖析搜索引擎的網(wǎng)絡(luò)爬蟲實(shí)現(xiàn)方法,Pyspider是一個(gè)開源項(xiàng)目、用Python語言編寫十分簡(jiǎn)潔且具有爬蟲程序的代表性,需要的朋友可以參考下2015-03-03python 和c++實(shí)現(xiàn)旋轉(zhuǎn)矩陣到歐拉角的變換方式
今天小編就為大家分享一篇python 和c++實(shí)現(xiàn)旋轉(zhuǎn)矩陣到歐拉角的變換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12