Skip to content

Intelligent Ticket Booking for Train 12306

Info

Author: Void, released on 2021-11-10, reading time: about 6 minutes, WeChat article link:

1. Introduction

During holidays, train tickets for going home or popular scenic spots are often sold out very quickly. In addition to relying on luck and speed to wait, we can also use a scientific method for intelligent ticket booking.

2. Project Introduction

Today's protagonist is the Train 12306 project on GitHub. The overall idea of the project is straightforward. It will continuously check whether there are tickets left for the desired train at the desired time, order them if available, reserve seats, and send users a reminder email to pay on time.

We also introduced the use of webdriver to control the browser and perform queries, clicks and other actions in the article Two-minute Build a Taobao Robot. So, what are the difficulties in the 12306 project?

We find that when users query too many times, 12306 will require us to click on the captcha.

For a real person, identifying these pictures is quite difficult, so how can the program identify them correctly? Here is a teaser, we'll discuss it later.

3. Project Usage

The project directory is shown below:

The usage of the project is relatively simple. We only need to change the TickerConfig.py configuration file in the root directory. We need to fill in the departure date, train number, departure and arrival cities, passenger information, and your email (for receiving and sending reminder emails). Please note that you need to add the passengers (which doesn't have to be you) to your 12306 account first. In addition, when you set COOKIE_TYPE = 1, the project will use webdriver to control the browser, open the 12306 official website, and let users log in. Therefore, you need to download the driver applicable to your browser and fill in the path in the configuration file. We recommend this method.

After filling in, we only need to run the following code to start the ticket grabbing task:

python run.py r

First, we need to log in to the 12306 account in the pop-up browser, then the program will automatically query the remaining tickets. The operation interface during the run is shown below:

When someone returns a ticket or new train tickets are released and there are remaining tickets, the program will automatically place an order and send an email to the user. We only need to pay in time.

4. How to Identify the Captcha

Recognizing captchas effectively is a difficult problem, and the project uses the code in this repo. The overall design idea of the project is:

  • Borrow Baidu's optical character recognition function and use a convolutional network-based character recognizer to recognize the Chinese characters to be found (in the example, it is an electronic scale and wind chime).
  • Then train an image classifier to recognize which category the image belongs to.
  • Use the result of the image classifier to continue training the character recognizer. The Chinese characters to be recognized must be among the results given by the image classifier.

The technologies involved include deep learning-related convolutional networks, image hash similarity judgment to find similar images. In simple terms, image hash similarity judgment converts the image into a grayscale image, calculates the size relationship between each pixel and the average grayscale value of n pixels, and if it is greater than a certain threshold, it is marked as 1; if it is less than the threshold, it is marked as 0. This comparison result can be converted into an integer. This one integer can then be used to compare the similarity of different images. Interested readers can search for related content and explore further.

5. Summary

In my limited (once and only once) use of this project, it helped me successfully book a train ticket. It also strongly reminded me that "high-tech" such as deep learning can really have an impact on our daily lives. Various complex algorithms are not just empty theories, but can truly provide value. I hope that interested readers can continue to explore on the road to knowledge, and I also hope that everyone can buy a ticket for their journey.


Viewed times

Comments