溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python使用selenium實(shí)現(xiàn)網(wǎng)頁(yè)用戶名 密碼 驗(yàn)證碼自動(dòng)登錄功能

發(fā)布時(shí)間:2020-10-04 03:32:03 來(lái)源:腳本之家 閱讀:372 作者:clarkxhb 欄目:開發(fā)技術(shù)

好久沒有學(xué)python了,反正各種理由吧(懶惰總會(huì)有千千萬(wàn)萬(wàn)的理由),最近網(wǎng)上學(xué)習(xí)了一下selenium,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的自動(dòng)登錄網(wǎng)頁(yè),具體如下。

1.安裝selenium:

如果你已經(jīng)安裝好anaconda3,直接在windows的dos窗口輸入命令安裝selenium:

python -m pip install --upgrade pip

查看版本pip show selenium

2.接著去http://chromedriver.storage.googleapis.com/index.html下載chromedriver.exe(根據(jù)chrome的版本下載對(duì)應(yīng)的)

3.將下載好的chromedriver.exe解壓后放到指定目錄

4.安裝tesseract-ocr.exe 配置環(huán)境變量

5.安裝pytesseract : pip install pytesseract

6.python腳本

思路:6.1登錄頁(yè)面按F12檢查元素,獲取用戶名 密碼 驗(yàn)證碼 驗(yàn)證碼圖片的元素id

   6.2.調(diào)用chromedriver

   6.3.截取驗(yàn)證碼圖片的位置

   6.4.pytesseract識(shí)別圖片中字符,最后驗(yàn)證碼識(shí)別為空?。????這個(gè)待解決

   6.5.腳本如下:

from selenium import webdriver
from PIL import Image
import pytesseract
import os,time
chromedriver = "D:\Program Files\Anaconda3\selenium\webdriver\chromedriver.exe" #這里寫本地的chromedriver 的所在路徑
os.environ["webdriver.Chrome.driver"] = chromedriver #調(diào)用chrome瀏覽器
driver = webdriver.Chrome(chromedriver)
driver.get("http://xxxx.com") #該處為具體網(wǎng)址
driver.refresh() #刷新頁(yè)面
driver.maximize_window() #瀏覽器最大化
#獲取全屏圖片,并截取驗(yàn)證碼圖片的位置
driver.get_screenshot_as_file('a.png')
location = driver.find_element_by_id('imgValidateCode').location
size = driver.find_element_by_id('imgValidateCode').size
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
a = Image.open("a.png")
im = a.crop((left,top,right,bottom))
im.save('a.png')
time.sleep(1)
#打開保存的驗(yàn)證碼圖片
image = Image.open("a.png")
#圖片轉(zhuǎn)換成字符
vcode = pytesseract.image_to_string(image)
print(vcode)
#填充用戶名 密碼 驗(yàn)證碼
driver.find_element_by_id("staffCode").send_keys("username")
driver.find_element_by_id("pwd").send_keys("password")
driver.find_element_by_id("validateCode").send_keys(vcode)
#點(diǎn)擊登錄 
driver.find_element_by_id("loginBtn").click()

總結(jié)

以上所述是小編給大家介紹的Python實(shí)現(xiàn)網(wǎng)頁(yè)用戶名 密碼 驗(yàn)證碼自動(dòng)登錄功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI