欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • python的格式化輸出(format,%)實(shí)例詳解

    python的格式化輸出(format,%)實(shí)例詳解

    Python中格式化字符串目前有兩種陣營:%和format,哪一種比較適合我們使用呢?下面腳本之家小編給大家介紹下python的格式化輸出(format,%)實(shí)例詳解,感興趣的朋友一起看看吧
    2018-06-06
  • python能自學(xué)嗎

    python能自學(xué)嗎

    在本篇文章里小編給大家整理了關(guān)于python如何自學(xué)的相關(guān)理論性文章,有需要的朋友們可以參考下。
    2020-06-06
  • 使用Python請(qǐng)求http/https時(shí)如何設(shè)置失敗重試次數(shù)

    使用Python請(qǐng)求http/https時(shí)如何設(shè)置失敗重試次數(shù)

    這篇文章主要介紹了使用Python請(qǐng)求http/https時(shí)如何設(shè)置失敗重試次數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Python實(shí)現(xiàn)將MySQL數(shù)據(jù)庫表中的數(shù)據(jù)導(dǎo)出生成csv格式文件的方法

    Python實(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-01
  • 基于python的Tkinter編寫登陸注冊(cè)界面

    基于python的Tkinter編寫登陸注冊(cè)界面

    這篇文章主要為大家詳細(xì)介紹了基于python的Tkinter編寫登陸注冊(cè)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Python?pycharm讀取文件相對(duì)路徑與絕對(duì)路徑的方法

    Python?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操作注冊(cè)表詳細(xì)步驟介紹

    Python操作注冊(cè)表詳細(xì)步驟介紹

    Python編程語言最大的特點(diǎn)在于其簡(jiǎn)單易用,可以大大方便開發(fā)人員的程序開發(fā)。在這里我們就一起來了解一下有關(guān)Python操作注冊(cè)表的相關(guān)應(yīng)用技術(shù)。Python操作注冊(cè)表相關(guān)的函數(shù)可以分為打開注冊(cè)表、關(guān)閉注冊(cè)表、讀取項(xiàng)值、c添加項(xiàng)值、添加項(xiàng),以及刪除項(xiàng)等幾類
    2020-02-02
  • 以Python的Pyspider為例剖析搜索引擎的網(wǎng)絡(luò)爬蟲實(shí)現(xiàn)方法

    以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-03
  • python 和c++實(shí)現(xiàn)旋轉(zhuǎn)矩陣到歐拉角的變換方式

    python 和c++實(shí)現(xiàn)旋轉(zhuǎn)矩陣到歐拉角的變換方式

    今天小編就為大家分享一篇python 和c++實(shí)現(xiàn)旋轉(zhuǎn)矩陣到歐拉角的變換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python引入其他py文件或模塊

    python引入其他py文件或模塊

    本文主要介紹了python引入其他py文件或模塊,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01

最新評(píng)論