广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python+selenium行为链登录12306(滑动验证码滑块)
  • 423
分享到

python+selenium行为链登录12306(滑动验证码滑块)

2024-04-02 19:04:59 423人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

使用python网络爬虫登录12306,网站界面如下。因为网站的反爬是不断升级的,以下代码虽然当前可用,但早晚必将会不再能满足登录需求。但是知识的价值,是不容置疑的。 from s

使用python网络爬虫登录12306,网站界面如下。因为网站的反爬是不断升级的,以下代码虽然当前可用,但早晚必将会不再能满足登录需求。但是知识的价值,是不容置疑的。

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秒钟,不要点的太快,以免被识别或者以免网页加载跟不上。
time.sleep(2)

# 滑动
# 定位 滑块标签
span = driver.find_element_by_id('nc_1_n1z')
actions = ActionChains(driver) # 行为链实例化
time.sleep(2) # 等待2秒钟

# 经截图测量,滑块需要滑过的距离为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)

如果不采取去除特征识别,即以下两行代码。则页面的滑块验证码在滑动后,会显示如下图的出错,从而阻止登录进行。因为服务器识别到的selenium的特征。使用该两行代码更改了特征,即可以顺利通过识别。

到此这篇关于Python+selenium行为链登录12306(滑动验证码滑块)的文章就介绍到这了,更多相关python+selenium行为链登录12306内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: python+selenium行为链登录12306(滑动验证码滑块)

本文链接: https://www.lsjlt.com/news/138779.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作