Python完全識(shí)別驗(yàn)證碼自動(dòng)登錄實(shí)例詳解
更新時(shí)間:2019年11月24日 09:31:08 作者:〆WangBenYan゜
今天小編就為大家分享一篇Python完全識(shí)別驗(yàn)證碼自動(dòng)登錄實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
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)文章
pytorch更新tensor中指定index位置的值scatter_add_問題
這篇文章主要介紹了pytorch更新tensor中指定index位置的值scatter_add_問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Python+OpenCV圖像處理之直方圖統(tǒng)計(jì)
直方圖就是對(duì)圖像的另外一種解釋,它描述了整幅圖像的灰度分布。通過直方圖我們可以對(duì)圖像的亮度、灰度分布、對(duì)比度等有了一個(gè)直觀的認(rèn)識(shí)。本文將為大家詳細(xì)介紹一下如何通過OpenCV實(shí)現(xiàn)直方圖統(tǒng)計(jì),感興趣的可以了解一下2021-12-12
Python實(shí)現(xiàn)帶圖形界面的炸金花游戲
詐金花又叫三張牌,是在全國廣泛流傳的一種民間多人紙牌游戲,它具有獨(dú)特的比牌規(guī)則。本文將通過Python語言實(shí)現(xiàn)帶圖形界面的詐金花游戲,需要的可以參考一下2022-12-12
python實(shí)現(xiàn)飛機(jī)大戰(zhàn)微信小游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)飛機(jī)大戰(zhàn)微信小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
解決Numpy報(bào)錯(cuò):ImportError: numpy.core.multiarray faile
這篇文章主要介紹了解決Numpy報(bào)錯(cuò):ImportError: numpy.core.multiarray failed問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
和孩子一起學(xué)習(xí)python之變量命名規(guī)則
這篇文章我們給大家總結(jié)了關(guān)于兒童學(xué)習(xí)python中的變量命名規(guī)則相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友跟著參考學(xué)習(xí)下。2018-05-05

