python+selenium行為鏈登錄12306(滑動驗證碼滑塊)
使用python網(wǎng)絡(luò)爬蟲登錄12306,網(wǎng)站界面如下。因為網(wǎng)站的反爬是不斷升級的,以下代碼雖然當前可用,但早晚必將會不再能滿足登錄需求。但是知識的價值,是不容置疑的。

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver import ChromeOptions
# 去除瀏覽器識別
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=option)
driver.get('https://kyfw.12306.cn/otn/resources/login.html')
# 解決特征識別
script = 'Object.defineProperty(navigator, "webdriver", {get: () => false,});'
driver.execute_script(script)
# 輸入賬號
driver.find_element_by_id('J-userName').send_keys('123@163.com')
# 輸入密碼
driver.find_element_by_id('J-password').send_keys('xxxxxxx')
# 點擊登陸
driver.find_element_by_id('J-login').click()
# 等待2秒鐘,不要點的太快,以免被識別或者以免網(wǎng)頁加載跟不上。
time.sleep(2)
# 滑動
# 定位 滑塊標簽
span = driver.find_element_by_id('nc_1_n1z')
actions = ActionChains(driver) # 行為鏈實例化
time.sleep(2) # 等待2秒鐘
# 經(jīng)截圖測量,滑塊需要滑過的距離為300像素
actions.click_and_hold(span).move_by_offset(300, 0).perform() # 滑動解決瀏覽器識別:
其中的以下這幾行代碼,可用去除瀏覽器對selenium的識別,如圖可以使瀏覽器頁面不再顯示圖中“Chrome正受到自動測試軟件的控制”字樣。
from selenium.webdriver import ChromeOptions
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=option)
解決特征識別的代碼:
script = 'Object.defineProperty(navigator, "webdriver", {get: () => false,});'
driver.execute_script(script)如果不采取去除特征識別,即以下兩行代碼。則頁面的滑塊驗證碼在滑動后,會顯示如下圖的出錯,從而阻止登錄進行。因為服務(wù)器識別到的selenium的特征。使用該兩行代碼更改了特征,即可以順利通過識別。

到此這篇關(guān)于python+selenium行為鏈登錄12306(滑動驗證碼滑塊)的文章就介紹到這了,更多相關(guān)python+selenium行為鏈登錄12306內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用 Python 和 Selenium 解決 Cloudflare 驗證碼的問題
- 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爬蟲selenium驗證之中文識別點選+圖片驗證碼案例(最新推薦)
相關(guān)文章
Python?OpenCV?Canny邊緣檢測算法的原理實現(xiàn)詳解
這篇文章主要介紹了Python?OpenCV?Canny邊緣檢測算法的原理實現(xiàn)詳解,由于邊緣檢測對噪聲敏感,因此對圖像應(yīng)用高斯平滑以幫助減少噪聲,具體詳情需要的小伙伴可以參考一下2022-07-07
循環(huán)神經(jīng)網(wǎng)絡(luò)TextRNN實現(xiàn)情感短文本分類任務(wù)
這篇文章主要為大家介紹了循環(huán)神經(jīng)網(wǎng)絡(luò)TextRNN實現(xiàn)情感短文本分類任務(wù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
使用XML庫的方式,實現(xiàn)RPC通信的方法(推薦)
下面小編就為大家?guī)硪黄褂肵ML庫的方式,實現(xiàn)RPC通信的方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

