Everyone should be very familiar with many train ticket grabbing software on the market, but few people have studied how to implement it, so they think it is mysterious, but it is actually very simple. The following uses Python to simulate the ticket grabbing program to reveal to you what the ticket grabbing process is.
This code is for reference only and is mainly used for communication. Commercial use is prohibited.
The specific code is as follows, you can modify it to your own 12306 username account:
The code is developed using Python Splinter. Splinter is an open source web application testing tool developed using Python. It can help you automatically browse and interact with the site. Splinter official website
http://splinter.readthedocs.io/en/latest/. When Splinter is executed, it will automatically open the browser you specify and access the specified URL. Then any behavior of the simulation you develop will be completed automatically. You only need to sit in front of the computer, watch the various actions on the screen automatically complete and collect the results like watching a movie.
Find the corresponding URL and find the control to simulate login, query, and ticket booking operations. The key is to find the name of the control. The difficulty is that the starting point is not the page value entered directly, and it needs to be found in the cookie.
Python code opens the URL and finds the control fill value:
Find the username and password control name
Find the starting control name
To determine the value of the starting location, use the "Check" function in the Chrome browser (press F12) and find it in Network ---> Cookies:
The value of the starting location in the cookie
Copy the cookie value of the starting place. I copied several commonly used cities and put them in the dictionary:
Query ticket code:
Actually, you just need to run the code:
Of course, what still needs to be manually clicked is the evil 12306 verification code. Just confirm the payment after grabbing the ticket.
Ticket grabbing is in progress
Success in grabbing tickets!
The above is the detailed content of Use Python code to automatically grab train tickets. For more information, please follow other related articles on the PHP Chinese website!