Home  >  Article  >  Python ticket grabbing program is optimized, you can select the train number and seat

Python ticket grabbing program is optimized, you can select the train number and seat

藏色散人
藏色散人forward
2019-01-21 11:12:204377browse

Use program automation to refresh and click to grab tickets, and you have this Python ticket grabbing program. This program is a Python simulation of manually operating a browser, so the program may terminate due to various network or other factors. This article mainly explains the two optimization points of adding train selection function and seat selection function.

Python ticket grabbing program is optimized, you can select the train number and seat

Recommended reference study: "Python Tutorial"

Automatic ticket grabbing process

1. First, sort out the optimized ticket grabbing process.

2. Automatically start the browser and automatically enter the 12306 login account and password set in the program.

3. Enter the verification code manually. The graphic verification code setting function is too complicated and involves artificial intelligence image recognition. If you do it yourself, the success rate is not high, so I will let you enter the verification code manually. Then manually click the "Login" button.

4. After successful login, the page will be automatically verified. After successful login is confirmed, it will automatically jump to the ticket checking page. Check tickets based on the departure place and destination entered in your own program code.

5. Based on the train number you entered, check whether the "Reservation" button on the right is highlighted and clickable. If it is not clickable, you will keep clicking the "Query" button and refresh the page until tickets appear and click the "Button" button.

6. Submit the order page, select the passenger, and select the seat type. If there is no type you want, for example, second-class seat, the page will return to the train ticket query page and query again, and so on.

7. After grabbing the tickets you want, submit the order, send the email, and done!

Login page

I maximized the browser window for this part. I didn’t set it to full screen before. Everyone’s computer display size is different, so some elements may be blocked. Unable to click.

After logging in, there may be a prompt about possible network problems. It is probably a server problem. At this time, manually click the return button in the upper left corner, and it will usually return to normal. If the prompt is still there after clicking once, then Click twice.

Python ticket grabbing program is optimized, you can select the train number and seat

Query page

Query the train ticket page. A timeout prompt will also appear during the peak time of this page. It is estimated that it is accessed Due to server anomalies caused by too many people, tickets can sometimes be found very quickly, but I can't find any specific patterns.

Python ticket grabbing program is optimized, you can select the train number and seat

We cannot avoid this. The program will automatically refresh for us until the page with tickets is refreshed. Refresh frequency: 1 second/time.

The train selection function is a function that everyone is more concerned about. Previously, order = 0 defaulted to all trains. This time, you can enter the corresponding number according to the position of the train. For example, the train to be booked is on the 5th line. You Just set order = 5. Regarding these parameter configurations, we will list them below.

Submit order page

Submit order page The program will do two things. The first is to select based on the passenger name you entered, and the second is to The judgment is based on the entered seats. If there are tickets for the seat you want, you will select and submit the order. If there are no tickets for the seat you want, you will return to the previous page, repeatedly query the tickets, check the seats, and so on until Book the tickets you want.

Python ticket grabbing program is optimized, you can select the train number and seat

Instructions for modifying important parameters

Here comes the key point. It doesn’t matter if you don’t understand it before. If you want to run it directly from the source code, be sure to read it carefully. Here, after getting the ticket grabbing source code, you need to pay attention to and configure the following things.

Python environment configuration

The Python environment I used for this program is Python 3.6. In fact, anything above 3.5 should be fine. Download the installation package of Python3 from the official website. When installing, remember to check the Add Python 3.x to PATH option. The advantage of checking this option is that after the installation is complete, it will automatically configure the environment variables for you, without the need for installing Python2.x. Configure manually. The next step is a fool-proof installation. After the installation is completed, if you enter Python in the terminal to display the version, the installation is successful. If you still don’t know how to do it, I suggest you check it out online. There are many online tutorials.

Python ticket grabbing program is optimized, you can select the train number and seat

pip tool

pip is a general Python library management tool. It is very convenient to use pip to install and manage libraries. After the first step is completed, the pip tool comes with it. You can enter the pip list command in the terminal to view all the Python libraries you are currently installing.

Python ticket grabbing program is optimized, you can select the train number and seat

Install and import the corresponding libraries

The main library used in this program is splinter. This library is mainly used to drive the browser to operate the page. Use the pip command to install it directly.

pip install splinter

The Chinese document address of Splinter related tutorials is as follows. If you are interested, you can learn about it.

https://splinter-docs-zh-cn.readthedocs.io/zh/latest/tutorial.html

After the installation is complete, use import to import this library in the IDE. If it still cannot be used, remember to install and introduce it in the editor. I use the Pycharm editor and introduce the installation in the settings, as shown in the picture below. .

Python ticket grabbing program is optimized, you can select the train number and seat

#Python To send emails, you need to use two modules that come with Python, smtplib and email. Import directly without downloading.

So, the only thing that really needs to be installed is the splinter library. Others can be imported directly and imported in the editor.

from splinter.browser import Browser
from time import sleep
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.header import Header

Download the browser driver and specify the path

This question comes up a lot. Why should you download the browser driver? Because the underlying principle of the splinter library is to drive the browser to perform corresponding operations through WebDriver, you must download the corresponding browser driver to use the corresponding browser. I use the chrome browser, so I downloaded chromedriver. There is a pitfall here. There is a mapping relationship between chromedriver and chrome browser. You must download the corresponding version. The mapping relationship is as follows

ChromeDriver v2.43 (2018-10-16)----------Supports Chrome v69-71
ChromeDriver v2.42 (2018-09-13)----------Supports Chrome v68-70
ChromeDriver v2.41 (2018-07-27)----------Supports Chrome v67-69
ChromeDriver v2.40 (2018-06-07)----------Supports Chrome v66-68
ChromeDriver v2.39 (2018-05-30)----------Supports Chrome v66-68
ChromeDriver v2.38 (2018-04-17)----------Supports Chrome v65-67
ChromeDriver v2.37 (2018-03-16)----------Supports Chrome v64-66
ChromeDriver v2.36 (2018-03-02)----------Supports Chrome v63-65
ChromeDriver v2.35 (2018-01-10)----------Supports Chrome v62-64

(Chrome and ChromeDriver mapping table)

After downloading and decompressing chromedriver, copy its path and replace the path in line 33 of the source code.

Python ticket grabbing program is optimized, you can select the train number and seat

Email notification function settings

After installing the corresponding library above, the information that needs to be filled in is as follows: sender and recipient The sender’s email address and the sender’s authorization code. Therefore, the positions that need to be modified are the sender's email account and recipient's email account in lines 51 and 52 in the sendMail function, and the authorization code of the sender's email in line 61.

Python ticket grabbing program is optimized, you can select the train number and seat


Authorization code acquisition method reference article 30 lines of Python code to automatically send and receive emails. The email address I use to send emails is My small QQ mailbox, and the recipient's mailbox is my large QQ mailbox. I tried it. The sender and recipient in QQ mailbox can be the same, that is, you can send emails to yourself. It is your choice. It is recommended to run the code for sending emails independently first to check whether the sending is successful. Generally The failure of the operation is caused by the incorrect authorization code. Please obtain the authorization code again.

The value corresponding to the seat

After debugging, it is found that the value corresponding to different seats is different.

Python ticket grabbing program is optimized, you can select the train number and seat


After many debuggings, I organized the value values ​​corresponding to different seats into a table.

Python ticket grabbing program is optimized, you can select the train number and seat


The code modification location is at lines 112 and 113 of the source code. Both lines must be modified to match the seats you want. The value will do.

12306 user name and 12306 login password

Just change these two items to your own in the main function. Just change lines 114 and 145 of the source code to your own.

Train selection

The trains are represented by the order field.
0 represents all trains, 1 represents the trains in the first row, and 2 represents the trains in the second row. number of trains, and so on. Generally, after you enter the starting point and destination, the locations of all trains are fixed. You can just enter the location number of the train you want to book. The source code modification location is also in the main function, line 146.

Passenger name format

Passenger name, such as passengers = ['XXX', 'XXX'], multiple selection is supported. Note that student tickets must be marked. Note The clear method is: passengers = ['XXX(student)', 'XXX'], otherwise an error will be reported. The source code modification location is also in the main function, line 149.

Ride date format

Ride date, the format is: '2019-01-28', the source code modification location is also in the main function, line 151.

Obtaining departure and destination cookies

Due to time issues, this has not been optimized. Currently, we can only manually search and obtain the departure and destination, and open our own 12306 Go to the train ticket query page and enter the departure point and destination. Turn on the debugging mode of the page, use the shortcut key F12 or right-click "Inspect" to turn on the debugging mode, select Network, and click the "Query" button to obtain the cookies corresponding to the origin and destination. The source code modification location is also in the main function, lines 153 and 155.

Python ticket grabbing program is optimized, you can select the train number and seat


To summarize, the above needs to be modified, the chromedriver path: in the initialization function init, the email notification function sending and receiving mailboxes The account number and sender authorization code are modified in the sendMail function, and the corresponding seat value is modified in the check_ticket function. Other information includes: 12306 user name, 12306 password, train selection, passenger name, ride date, departure date, departure place and destination cookie values ​​are modified in the main function at the bottom of the source code.

Python ticket grabbing program is optimized, you can select the train number and seat


##Important Note: Due to limited time, this program has a lot of configuration items, but in order to grab Ticket, we can only try one more method. This program simulates manual operation of the browser. There will still be many problems. For example, due to the network relationship, the page will stay for a long time and the element cannot be found and the program will stop. Various other problems. Unexpected problems will also occur. If the program stops, you can only restart the program and continue to grab it. I will slowly optimize it later if you need it.


Statement:
This article is reproduced at:Python知识圈. If there is any infringement, please contact admin@php.cn delete