Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to log in to 12306 with python behavior chain

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to log in 12306 with python behavior chain". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Log in to 12306 using the python web crawler and the website interface is as follows. Because the anti-crawling of the site is constantly upgraded, the following code is currently available, but sooner or later will no longer be able to meet the login requirements. But the value of knowledge is beyond doubt.

From selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport timefrom selenium.webdriver import ChromeOptions# removes browser recognition 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')# solution feature recognition script =' Object.defineProperty (navigator, "webdriver") {get: () = > false,}) 'driver.execute_script (script) # enter the account driver.find_element_by_id (' Jmurusername'). Send_keys ('123 password 163.com') # enter the password driver.find_element_by_id ('Jmurpassword'). Send_keys ('xxxxxxx') # Click to log in to driver.find_element_by_id (' Jmurlogin'). Click () # wait 2 seconds, don't click too fast To avoid being identified or lest the web page load can't keep up. Time.sleep (2) # Slide # Positioning Slider label span = driver.find_element_by_id ('nc_1_n1z') actions = ActionChains (driver) # behavior chain instantiation time.sleep (2) # wait 2 seconds # after screenshot measurement, the slider needs to slide over a distance of 300 pixels actions.click_and_hold (span). Move_by_offset (300,0). Perform () # slide

Resolve browser recognition:

The following lines of code can be used to remove the browser's recognition of selenium, such as the figure so that the browser page can no longer display the words "Chrome is under the control of automated testing software".

From selenium.webdriver import ChromeOptionsoption = ChromeOptions () option.add_experimental_option ('excludeSwitches', [' enable-automation']) option.add_experimental_option ("detach", True) driver = webdriver.Chrome (options=option)

Code to solve feature recognition:

Script = 'Object.defineProperty (navigator, "webdriver", {get: () = > false,});' driver.execute_script (script)

If the removal of feature recognition is not taken, that is, the following two lines of code. The slider CAPTCHA of the page will display the error shown in the following figure after sliding, thus preventing the login from proceeding. Because of the characteristics of selenium recognized by the server. Use these two lines of code to change the characteristics, that is, you can successfully pass the recognition.

"how to use the python behavior chain to log in 12306" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report