Introduction:

Seeing the National Day on October 1 just have a whole month, general xiaobian is a few days before going home to buy a ticket, that at the peak of this kind of home to buy tickets? Xiaobian own way!

​​

So did you go home and get tickets? Is a careless, home ticket was magically robbed of empty?

​​

Don’t worry guys, what are we learning Python for? It’s all about solving these so-called problems. So finish this tutorial and let those days of watching your computer carefully to get tickets go away!! No matter it’s holiday or weekend, don’t worry about your hometown

How to use Python to get tickets home!!

The body of the

(1) Users can get the information they want simply by entering the departure station, arrival station, and date. Names like tickets fit perfectly:

$ tickets from to date

(2) There are various types of trains, such as high-speed train, bullet train, express train, express train and direct train.

  • – g high-speed rail

  • – d train

  • T express

  • – k quick

  • Z direct

(3) Combination of the following interfaces:

$ tickets [-gdtkz] from to date

(4) Environmental installation:

Write a Python program to create a virtual environment using the virtualenv tool.

To create a virtual environment, create a folder named tickets under your working directory:

$ virtualenv -p /usr/bin/python3 venv

Activate it with the command $.venv /bin/activate

(5) Parsing parameters: Python has many tools for writing command line applications. Here, we will use the easy-to-use tool docopt. We will install it first:

$ pip3 install docopt

Docopt can parse parameters in the format we define in docstrings, in tickets.py:

(6) Let’s run this program:

$python3 tickets.py Shanghai Beijing 2016-07-01

We get the following parameter parsing results:

(7) Data acquisitionIf you use Chrome, press F12 to open the developer tool, select the “Network” bar, in the query box we enter the address and date will appear in the URL address.

Returns data in Json format! Then we just need to build the request URL and parse the returned Json data. But we found that from_station and to_station in the URL were not Chinese characters, but code names, and the user typed Chinese characters, and then

We found this link on the web page:

https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8955
Copy the code

It seems to contain the Chinese names, pinyin, abbreviations and code names of all the stations. We saved it as stations.html under the project directory.

Using regular expressions directly, we can write a small script to match and extract the desired information, in parse.py:

(8)We run the script, which returns all stations and their capital code names in dictionary form, and we redirect the results to stations.py:

$ python3 parse.py > stations.py

We name the dictionary, stations, and finally the stations.py file looks like this:

Now the user enters the Chinese name of the station and we can get its alphabetic code directly from the dictionary:

(9) Let’s request this URL to get the data! Here we use the Requests library:

$ pip3 install requests

Further advance ticket information:

​​

Prettytable is a library that allows us to format and display data like a MySQL database.

$ pip3 install prettytable

Use it like this:

Python code:

**1.** Implements automatic login functions:

2. Start the ticket buying function:

Effect:

conclusion

Source code base: #959755565# reply “grab ticket assistant small program” can be obtained for free oh ~

Welcome to read the previous article, remember sanlian, love you!