1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python爬虫selenium行为链登录12306(滑动验证码滑块)(解决特征识别)

python爬虫selenium行为链登录12306(滑动验证码滑块)(解决特征识别)

时间:2018-11-02 09:58:21

相关推荐

python爬虫selenium行为链登录12306(滑动验证码滑块)(解决特征识别)

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

from selenium import webdriverfrom mon.action_chains import ActionChainsimport timefrom 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('/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@')# 输入密码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 ChromeOptionsoption = 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的特征。使用该两行代码更改了特征,即可以顺利通过识别。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。