In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how Python realizes the 12306 automatic train ticket grabbing function. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
First, the effect display
Before officially entering the code explanation, let's take a look at the implementation effect of this article.
If it is not to demonstrate the effect, just add a delay click OK in the final determination phase, you should be able to lock a ticket in less than 45 seconds, as long as you pay within 30 minutes.
Second, detailed explanation of the code
This section will unlock in detail how the ticket-grabbing software simulates logging in to the website and buys tickets automatically. In order to show you more clearly, part of the code is not written as a function, just run naked code, so that friends who need to buy tickets can use software to buy tickets.
1 Import Library
First of all, import the libraries that need to be loaded in this article. If you have some libraries that have not been installed, which leads to errors in running code, you can install them in Anaconda Prompt using the pip method.
Import jsonimport timefrom captcha import * from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import waitfrom selenium.webdriver.common.keys import Keysfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.support import expected_conditions as EC# Import Library 2 determines the basic information of ticket purchase
After importing the library, fill in the basic information of your ticket purchase in the python code.
Purpose = 'ADULT' # Buy adult tickets, if student tickets Need to adjust the code names = ['Xie Chaoyang'] # fill in the buyer's name date = '2021-09-21' # fill in the date of purchase start_station = 'Shenzhen' # purchase departure station end_station = 'Changsha South' # purchase destination station password = '11234567xyz' # login secret username =' xiezhaoyang122700' # login 12306 account trains = ['G1004'' 'G80 flights, 'G6028 flights,' G6182 flights, 'G6016'] # the number of flights you want to buy # fill in the basic information
The ticket booked in this article is a high-speed train ticket from Shenzhen to Changsha South on September 21, 2021. You can adjust it according to your actual needs. As some flights are too early or too late, it is very inconvenient to buy them, so you can choose the ones you are satisfied with in trains to buy tickets. Here, I need to remind you that the pit I encountered when I tried the code before, that is, if there is a digit in the time, I should fill in 0 in front of it. For example, on September 2, 2021, you have to write '2021-09-02' when you fill in the ticket date date, otherwise the date will not always be filled in when you run the code.
3 Log in to 12306
After determining the basic information of ticket purchase, you can log in to 12306 using python simulation, as follows:
Options= webdriver.ChromeOptions () options.add_argument ("--disable-blink-features=AutomationControlled") browser = webdriver.Chrome (options=options) browser.maximize_window () login_url = 'https://kyfw.12306.cn/otn/resources/login.html'#ticket_url =' https://kyfw.12306.cn/otn/leftTicket/init'browser.get(login_url)time.sleep(0.5)wait.WebDriverWait(browser, 5). EC.element_to_be_clickable ((By.CLASS_NAME) ). Click () input_name = browser.find_element_by_id ('Jmurusername') input_pd = browser.find_element_by_id ('Jmurpassword') input_name.send_keys (username) input_pd.send_keys (password) login = browser.find_element_by_id (' Jmurlogin') login.click () # Login 12306
The overall idea is:
1. Using python to simulate calling google browser
two。 Enter 12306 URL
3. Click the account password to log in after the web page is loaded completely.
4. Find the id of the account password and fill in the account password information
5. Find the login id and simulate and click the login button.
Two points should be paid attention to in this section.
First, put the chromedriver that matches the google version in the python installation directory for python to call.
Second, learn to find the id that fill in the account password information.
First, enter the 12306 login URL in the google browser:
Https://kyfw.12306.cn/otn/resources/login.html
Then click the account password to log in, and the following interface will appear:
Then click the three points in the red box to find more tools, click developer tools, and the following interface will appear:
Click the arrow in the red box, move the mouse over the account box, and the following interface will appear:
The corresponding id will appear in the gray box on the right, click on the account box, then move the mouse to the gray character on the right, right click, the option of copy element will appear, and copy it.
Did you find out? The browser.find_element_by_id ('Jmurusername') to be filled in in the source code input_name is the information in the id= "J-userName".
4 simulated slider
Run the following code to drag the slider to verify.
Browser.implicitly_wait (5) print ('= start processing sliding CAPTCHA =') track = [300,400,500] for i in track: btn = browser.find_element_by_xpath ('/ / * [@ id= "nc_1__scale_text"] / span') ActionChains (browser). Drag_and_drop_by_offset (btn,i,0). Perform () except: time.sleep (2) # pull slider verification
Among them, browser.implicitly_wait (5) indicates a hidden wait of 5 seconds, and the distance pulled by the slider is put in the track.
5 Special requirements for dealing with epidemic situation
Click OK with the following code.
Browser.implicitly_wait (5) browser.find_element_by_xpath ('/ html/body/div [5] / div [2] / div [3] / a'). Click () time.sleep (2) # Special requirements for epidemic situation
The difference between browser.find_element_by_xpath and id is that you want copy XPath or copy full XPath when you right-click to copy.
6 Click to purchase the ticket and fill in the departure place, destination and departure time
The next step is to choose to buy a ticket and fill in the departure, destination, departure time and other information.
Browser.find_element_by_xpath ('/ / * [@ id= "J-chepiao"] / a'). Click () browser.find_element_by_xpath ('/ / * [@ id= "megamenu-3"] / div [1] / ul/li [1] / a') .click () browser.find_element_by_xpath ('/ / * [@ id= "qd_closeDefaultWarningWindowDialog_id"]') .click () # choose to buy a ticket def input_info (): print ('= start buying tickets =') from_station = browser.find_element_by_xpath ('/ / * [@ id= "fromStationText"]') from_station.send_keys (Keys.ENTER) from_station.send_keys (Keys.CONTROL 'a') from_station.send_keys (start_station, Keys.ENTER) browser.implicitly_wait (5) to_station = browser.find_element_by_xpath ('/ * [@ id= "toStationText"]') to_station.send_keys (Keys.ENTER) to_station.send_keys (Keys.CONTROL,'a') to_station.send_keys (end_station Keys.ENTER) browser.implicitly_wait (5) start_date = browser.find_element_by_xpath ('/ / * [@ id= "train_date"]') start_date.send_keys (Keys.ENTER) start_date.send_keys (Keys.CONTROL,'a') start_date.send_keys (Keys.CONTROL,'x') start_date.send_keys (date, Keys.ENTER) browser.implicitly_wait (5) wait.WebDriverWait (browser 3) .By.ID,'query_ticket' (EC.element_to_be_clickable ((By.ID,'query_ticket') .click () input_info () input_info () # fill in the place of departure, destination and date of departure
The results are as follows:
It is important to note that I called the input_info function twice, because 12306 may have taken some anti-crawling measures. After entering it again, I can't find anything and it is grayed out.
7 lock the ticket
Finally, it is in turn to find out whether the train in trains has a ticket, and if so, click to buy a locked ticket.
LeftTicket) if leftTicket = 'you' or leftTicket.isdigit (): orderBtn = tr.find_element_by_class_name ("btn72") orderBtn.click () browser.implicitly_wait (5) passengerLabels = browser.find_elements_by_xpath (". / / ul [@ id='normal_passenger_id'] / li/label") for passengerLabel in passengerLabels: Name = passengerLabel.text if name in names: passengerLabel.click () browser.implicitly_wait (20) # get submit button submitBtn = browser.find_element_by_id ("submitOrder_id") submitBtn.click () browser.implicitly_wait (20) ConfirmBtn = browser.find_element_by_id ("qr_submit_id") confirmBtn.click () time.sleep (2) browser.implicitly_wait (20) confirmBtn = browser.find_element_by_id ("qr_submit_id") confirmBtn.click () break # find whether there are tickets in trains in turn If so, click to buy this article on "how to achieve 12306 automatic train ticket grabbing function in Python". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.