Personal WeChat account: Xiao Zhang Python, welcome everyone to pay attention!
Hello everyone, I am zeroing~
It's time for the Chinese New Year soon. Have you all bought tickets to go home? At the end of the year, the ticket becomes particularly difficult to obtain. This year, because of the epidemic control and control may be better than in previous years, but it is still difficult to grab,
For small partners who travel relatively far away, once they miss the best time to release the tickets, the possibility of grabbing tickets later is very small, because most of the people who compete with you to grab tickets are not people but automated programs. In one second The one that can complete the ticket purchase and place an order within (of course, it is also on the premise of having a ticket)
Today, I will introduce to you an open source project related to 12306 ticket grabbing, and teach you how to run the program. I hope we can all grab tickets to go home.
Searching for the keyword 12306 on Github can find many related open source projects, the highest star is 29.3k, but according to the project update log (the last update was 13 months ago), the timeliness needs to be verified
The configuration process of this project is very simple, you only need to configure the user name and password. I tried to run it locally; the verification code recognition step is no problem, but the login (cookie automatic collection verification) step is not good
Two days ago to see the early Gangster (No public: get up early Python) shared an article about another project 12306 grab votes, author of the project jar of tea on the basis of the above bug on this project made some changes, compared to their original configuration project more than a step configuration Cookie information ;
Star currently has 260+, and I have tested it myself. If it is used to grab tickets, there is no problem. The effect is as follows;
Below I will introduce the specific configuration steps of this project
1, Download the warehouse to the local
The Github address of the project: https://github.com/gzldc/12306
First, download the original project locally. It is recommended not to use the git command when downloading, it will be slower, just click the Download ZIP button below.
Decompress the downloaded project, the decompressed directory is as follows (there is no venv folder, venv refers to the virtual environment)
I need to say something here, because the project requires dependency Tensorflow, Tensorflow currently only supports Python 3.6-3.8, and it is 64-bit, please make sure that your Python version meets this requirement; I use Python 3.7.6, 64-bit, Tested, everything is ok
2. Create a virtual environment and install dependencies
Create a virtual environment of the primary purpose of configuration items needed to prevent conflicts with the system's own project, create a virtual environment for the project, you can use pipenv, virtualenv command line, can also help Pycharm the Project Interpreter
aid of configuration options when I configure Is Pycharm
Install dependencies , open a terminal in the project root directory, and enter the following commands in the virtual environment to configure the dependencies required by the project
pip install -r requirements.txt
3. Add Cookie and change project configuration
After dependency configuration is complete, open the project file directory under TickerConfig.py
the file is stored in the configuration information required by the program at about 50-55 rows, find the three parameters tk
, RAIL_EXPIRATION
, RATI_DEVICEID
, information of these three parameters are Cookie, the need to manually Configuration
How to find cookies? 12306 First open a browser, log in with their own account, after the completion of shortcut keys F12
or right mouse button to find 审查
options, click on the page developer mode, Network-> XHR , as shown below
If the page on the upper right is empty, just refresh the page.
Click XHR in initMy ... page, Request Headers
under the option Cookies
information is required for the program
Cookie needs only one of the three, find tk
, RAIL_EXPIRATION
, RATI_DEVICEID
three, in order to fill in the configuration file, pay attention to the string before the semicolon after the equal sign simply fill in
After the cookie is configured, the next step is to configure some basic information, such as date, passenger, seat, etc., re-enter the TickerConfig.py
script
TICKET_TYPE
, Swipe mode, (default 1 is fine),SATION_DATES
, Departure dateOPEN_TIME
, The pre-sale ticket time (recommended to be empty);
- FROM_STATION, starting point
- TO_STATION, destination
- SET_TYPE, seat
- IS_MORE_TICKET, fill True for single sheet and False for multiple sheets
- TICKET_PEOPLES, passenger information in your account (need to be configured in the account in advance)
- USER, the default is fine, no need to fill in
- PWD, the default is fine, no need to fill in
- tk, RAIL_EXPIRATION, RAIL_DEVICEID, the content in Cookie, please refer to the configuration tutorial mentioned earlier in the article;
- The following
11
characters are
The parameters that need to be changed in the configuration are the ones mentioned above. The environment configuration is basically OK here, and the next step is to start the program
4. Start the program and start to grab tickets
At the terminal, first enter
python run.py c
Adding cdn, the agent pool is used in the program, so start the agent pool in advance, and remove some unusable agents. The effect of cdn is as follows (this step takes a relatively long time, please wait patiently)
After the cdn is loaded, enter the following command
python run.py r
Start the script to grab tickets, the final effect is as follows:
After grabbing the ticket, the user needs to complete the payment within 30 minutes, otherwise the ticket will be recycled by the system; therefore, email reminders, WeChat reminders and other functions have been added to the program. I did not introduce them in detail here. If you are interested, you can Research it yourself
5. Postscript
Although the above steps are very detailed, there may still be some bugs during the first deployment due to a little difference in the environment configuration. The following two are what I encountered in the configuration, friends can refer to it (no encounter To better)
1,AttributeError : ‘str’ object has no attribute 'decode’
The error message is as follows
model_config = json.loads(model_config.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'
The above error occurs because the installation of a package h5py
version is too high,
Solution: By pip install h5py==2.10.0
reducing the release can be;
2. tk is invalid
Because the Cookie information in step 3 has expired, the solution is to reconfigure tk according to the method in step 3, and the latter two parameters will basically not change;
The above is the entire content of this article. I hope the article will be helpful to you. Finally, thank you for reading!
Disclaimer, this tutorial is only used for personal study and research, and must not be used for any commercial purposes, otherwise you will be responsible for the consequences!