python+selenium識(shí)別驗(yàn)證碼并登錄的示例代碼
由于工作需要,登錄網(wǎng)站需要用到驗(yàn)證碼。最初是研究過(guò)驗(yàn)證碼識(shí)別的,但是總是不能獲取到我需要的那個(gè)驗(yàn)證碼。直到這周五,才想起這事來(lái),昨天順利的解決了。
下面正題:
python版本:3.4.3
所需要的代碼庫(kù):PIL,selenium,tesseract
先上代碼:
#coding:utf-8
import subprocess
from PIL import Image
from PIL import ImageOps
from selenium import webdriver
import time,os,sys
def cleanImage(imagePath):
image = Image.open(imagePath) #打開(kāi)圖片
image = image.point(lambda x: 0 if x<143 else 255) #處理圖片上的每個(gè)像素點(diǎn),使圖片上每個(gè)點(diǎn)“非黑即白”
borderImage = ImageOps.expand(image,border=20,fill='white')
borderImage.save(imagePath)
def getAuthCode(driver, url="http://localhost/"):
captchaUrl = url + "common/random"
driver.get(captchaUrl)
time.sleep(0.5)
driver.save_screenshot("captcha.jpg") #截屏,并保存圖片
#urlretrieve(captchaUrl, "captcha.jpg")
time.sleep(0.5)
cleanImage("captcha.jpg")
p = subprocess.Popen(["tesseract", "captcha.jpg", "captcha"], stdout=\
subprocess.PIPE,stderr=subprocess.PIPE)
p.wait()
f = open("captcha.txt", "r")
#Clean any whitespace characters
captchaResponse = f.read().replace(" ", "").replace("\n", "")
print("Captcha solution attempt: " + captchaResponse)
if len(captchaResponse) == 4:
return captchaResponse
else:
return False
def withoutCookieLogin(url="http://org.cfu666.com/"):
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url)
while True:
authCode = getAuthCode(driver, url)
if authCode:
driver.back()
driver.find_element_by_xpath("http://input[@id='orgCode' and @name='orgCode']").clear()
driver.find_element_by_xpath("http://input[@id='orgCode' and @name='orgCode']").send_keys("orgCode")
driver.find_element_by_xpath("http://input[@id='account' and @name='username']").clear()
driver.find_element_by_xpath("http://input[@id='account' and @name='username']").send_keys("username")
driver.find_element_by_xpath("http://input[@type='password' and @name='password']").clear()
driver.find_element_by_xpath("http://input[@type='password' and @name='password']").send_keys("password")
driver.find_element_by_xpath("http://input[@type='text' and @name='authCode']").send_keys(authCode)
driver.find_element_by_xpath("http://button[@type='submit']").click()
try:
time.sleep(3)
driver.find_element_by_xpath("http://*[@id='side-menu']/li[2]/ul/li/a").click()
return driver
except:
print("authCode Error:", authCode)
driver.refresh()
return driver
driver = withoutCookieLogin("http://localhost/")
driver.get("http://localhost/enterprise/add/")
怎么獲取我們需要的驗(yàn)證碼
在這獲取驗(yàn)證碼的道路上,我掉了太多的坑,看過(guò)太多的文章,很多都是教你驗(yàn)證碼的識(shí)別方法,但是沒(méi)有說(shuō)明,怎么獲取你當(dāng)前需要的驗(yàn)證碼圖片。
我的處理方法是:
1.先用selenium打開(kāi)你需要的登錄的頁(yè)面地址url1

2.通過(guò)審核元素獲取驗(yàn)證碼的地址url2(其實(shí)最簡(jiǎn)單的是右鍵打開(kāi)新頁(yè)面)

3:在url1頁(yè)面,輸入地址url2進(jìn)入url2頁(yè)面,然后截屏保存驗(yàn)證碼頁(yè)面

4:處理驗(yàn)證碼得到驗(yàn)證碼字符串。然后點(diǎn)擊瀏覽器后退按鈕,返回url1登錄頁(yè)面
5:輸入登錄需要的信息和驗(yàn)證碼

6:點(diǎn)擊登錄
7:驗(yàn)證登錄后的頁(yè)面,判斷是否成功,若不成功則需要重新1-7的操作。
為了保護(hù)公司的信息,這個(gè)頁(yè)面是我本地搭的服務(wù),我在伯樂(lè)在線注冊(cè)頁(yè)面進(jìn)行測(cè)試過(guò)這個(gè)驗(yàn)證碼獲得方法,可以通過(guò)。(這個(gè)驗(yàn)證碼的處理方法,僅限驗(yàn)證碼背景是像素點(diǎn),若驗(yàn)證碼有橫線需額外處理。)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用 Python 和 Selenium 解決 Cloudflare 驗(yàn)證碼的問(wèn)題
- python+selenium行為鏈登錄12306(滑動(dòng)驗(yàn)證碼滑塊)
- Python Selenium破解滑塊驗(yàn)證碼最新版(GEETEST95%以上通過(guò)率)
- Python +Selenium解決圖片驗(yàn)證碼登錄或注冊(cè)問(wèn)題(推薦)
- Selenium+Python 自動(dòng)化操控登錄界面實(shí)例(有簡(jiǎn)單驗(yàn)證碼圖片校驗(yàn))
- selenium+python實(shí)現(xiàn)1688網(wǎng)站驗(yàn)證碼圖片的截取功能
- Python使用selenium實(shí)現(xiàn)網(wǎng)頁(yè)用戶名 密碼 驗(yàn)證碼自動(dòng)登錄功能
- Python Selenium Cookie 繞過(guò)驗(yàn)證碼實(shí)現(xiàn)登錄示例代碼
- Python爬蟲(chóng)selenium驗(yàn)證之中文識(shí)別點(diǎn)選+圖片驗(yàn)證碼案例(最新推薦)
相關(guān)文章
如何利用python實(shí)現(xiàn)圖片轉(zhuǎn)化字符畫(huà)
這篇文章主要介紹了如何利用python實(shí)現(xiàn)圖片轉(zhuǎn)化字符畫(huà),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
PyCharm之如何設(shè)置自動(dòng)換行問(wèn)題
這篇文章主要介紹了PyCharm之如何設(shè)置自動(dòng)換行問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
VSCode2022配置Python3.9.6的詳細(xì)教程
這篇文章主要介紹了VSCode2022配置Python3.9.6教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
python pandas輕松通過(guò)特定列的值多條件去篩選數(shù)據(jù)及contains方法的使用
這篇文章主要介紹了python pandas輕松通過(guò)特定列的值多條件去篩選數(shù)據(jù)及contains方法的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Python Requests.post()請(qǐng)求失敗時(shí)的retry設(shè)置方式
這篇文章主要介紹了Python Requests.post()請(qǐng)求失敗時(shí)的retry設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Pytorch生成隨機(jī)數(shù)Tensor的方法匯總
這篇文章主要介紹了Pytorch生成隨機(jī)數(shù)Tensor的方法匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
python中count函數(shù)知識(shí)點(diǎn)淺析
在本篇文章里小編給大家整理了一篇關(guān)于python中count函數(shù)知識(shí)點(diǎn)淺析內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-12-12

