python+selenium識別驗證碼并登錄的示例代碼
由于工作需要,登錄網(wǎng)站需要用到驗證碼。最初是研究過驗證碼識別的,但是總是不能獲取到我需要的那個驗證碼。直到這周五,才想起這事來,昨天順利的解決了。
下面正題:
python版本:3.4.3
所需要的代碼庫: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) #打開圖片 image = image.point(lambda x: 0 if x<143 else 255) #處理圖片上的每個像素點,使圖片上每個點“非黑即白” 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/")
怎么獲取我們需要的驗證碼
在這獲取驗證碼的道路上,我掉了太多的坑,看過太多的文章,很多都是教你驗證碼的識別方法,但是沒有說明,怎么獲取你當前需要的驗證碼圖片。
我的處理方法是:
1.先用selenium打開你需要的登錄的頁面地址url1
2.通過審核元素獲取驗證碼的地址url2(其實最簡單的是右鍵打開新頁面)
3:在url1頁面,輸入地址url2進入url2頁面,然后截屏保存驗證碼頁面
4:處理驗證碼得到驗證碼字符串。然后點擊瀏覽器后退按鈕,返回url1登錄頁面
5:輸入登錄需要的信息和驗證碼
6:點擊登錄
7:驗證登錄后的頁面,判斷是否成功,若不成功則需要重新1-7的操作。
為了保護公司的信息,這個頁面是我本地搭的服務,我在伯樂在線注冊頁面進行測試過這個驗證碼獲得方法,可以通過。(這個驗證碼的處理方法,僅限驗證碼背景是像素點,若驗證碼有橫線需額外處理。)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 使用 Python 和 Selenium 解決 Cloudflare 驗證碼的問題
- python+selenium行為鏈登錄12306(滑動驗證碼滑塊)
- Python Selenium破解滑塊驗證碼最新版(GEETEST95%以上通過率)
- Python +Selenium解決圖片驗證碼登錄或注冊問題(推薦)
- Selenium+Python 自動化操控登錄界面實例(有簡單驗證碼圖片校驗)
- selenium+python實現(xiàn)1688網(wǎng)站驗證碼圖片的截取功能
- Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能
- Python Selenium Cookie 繞過驗證碼實現(xiàn)登錄示例代碼
- Python爬蟲selenium驗證之中文識別點選+圖片驗證碼案例(最新推薦)
相關文章
python pandas輕松通過特定列的值多條件去篩選數(shù)據(jù)及contains方法的使用
這篇文章主要介紹了python pandas輕松通過特定列的值多條件去篩選數(shù)據(jù)及contains方法的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02Python Requests.post()請求失敗時的retry設置方式
這篇文章主要介紹了Python Requests.post()請求失敗時的retry設置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08