Have you bought a ticket to go home? I will teach you how to grab a ticket with Python today

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

image-20210116154318822

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

image-20210116154632274

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;

Snipaste_2021-01-16_12-00-19

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.

Snipaste_2021-01-16_13-43-22

Decompress the downloaded project, the decompressed directory is as follows (there is no venv folder, venv refers to the virtual environment)

image-20210116134810028

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 Interpreteraid of configuration options when I configure Is Pycharm

virtualenv

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

1

3. Add Cookie and change project configuration

After dependency configuration is complete, open the project file directory under TickerConfig.pythe 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

Cookie

How to find cookies? 12306 First open a browser, log in with their own account, after the completion of shortcut keys F12or right mouse button to find 审查options, click on the page developer mode, Network-> XHR , as shown below

image-20210116144205757

If the page on the upper right is empty, just refresh the page.

Click XHR in initMy ... page, Request Headersunder the option Cookiesinformation is required for the program

image-20210116144503667

Cookie needs only one of the three, find tk, RAIL_EXPIRATION, RATI_DEVICEIDthree, in order to fill in the configuration file, pay attention to the string before the semicolon after the equal sign simply fill in

image-20210116145013283

After the cookie is configured, the next step is to configure some basic information, such as date, passenger, seat, etc., re-enter the TickerConfig.pyscript

image-20210116145344344

  • TICKET_TYPE, Swipe mode, (default 1 is fine),
  • SATION_DATES, Departure date
  • OPEN_TIME , The pre-sale ticket time (recommended to be empty);

image-20210116145728654

  • FROM_STATION, starting point
  • TO_STATION, destination
  • SET_TYPE, seat
  • IS_MORE_TICKET, fill True for single sheet and False for multiple sheets

image-20210116150728952

  • 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 11characters 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)

Screening CDN

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:

22222

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 h5pyversion is too high,

Solution: By pip install h5py==2.10.0reducing the release can be;

2. tk is invalid

image-20210116153011724

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!

Guess you like

Origin blog.csdn.net/weixin_42512684/article/details/112765991