Use Python and splinter to achieve 12306 train ticket enquiries and robbery tickets

Source: Internet
Author: User
Tags windows x86 chrome developer chrome developer tools

There is no use of Python for some time, a few days ago, a friend brought up a piece about the use of Python to rob the train ticket article, Baidu to achieve the technical details of the train ticket, online but there is a lot of information, is not fresh things. In the understanding of some technical means, read some of the Great God's blog post, also tried to achieve a bit, the code is coarse, pure when entertainment, this article in the Windows system to complete. It is necessary to mention that the verification code part of the ticket process can only be done manually.

First, the tools and components I need are:

    • Chrome browser
    • Browser driver Chromedriver
    • Python 3.5
    • Web Application Test Tool splinter

Chrome browser can be downloaded by itself, and its browser driver Chromedriver can be found at the following link: http://chromedriver.storage.googleapis.com/index.html?path=2.20/

Download Python on the official website, choose Windows x86 executable installer, install the idle, Pip and so on together, tick the option to add Python as environment variable (run python under Windows cmd, Environment variables need to be set). After the installation is complete, execute: Pip install splinter install splinter.

The basic configuration is complete, test it, run Python under CMD, and type the following instructions, respectively:

>>> fromimport Browser>>> b = Browser(driver_name="chrome")>>> b.visit("http://www.qq.com")

You can create a basic file that writes the above three sentences:

You will need to use Google Chrome developer tools to call "F12" or "shift+ctrl+i" when performing subsequent operations. Find the Elements tab, which records information about each element of the current page, such as the following search bar and the "Baidu button" button, you can select the page features or location you want to view, elements will jump to the corresponding implementation code.

The Search bar name = "WD", "Baidu" button id = "Su", after obtaining this information, you can perform the following three steps:
1. Enter: b.fill("wd", "splinter") You can search for splinter in the search bar.
2. Enter: button = b.find_by_value(u"百度一下") Find the button
3. Enter: button.click() Click the button

For more information, refer to Splinter's official documentation ...

Here's how to get to the 12306 URL:

Similar to the operation on the Baidu page, enter personal information in the user name and password fields:

b.find_by_text(u"登录").click()b.fill("loginUserDTO.user_name"# username部分输入自己的账号b.fill("userDTO.password"# passwd部分输入账号密码

The reason is that the semi-automatic robbery, because before the ticket to collect some information manually, such as manually select a departure location, date and arrival location and other information, and then F12 Open the Developer tool, click Resources, find the cookie option, you can see the content shown:

By using cookies, you can see the manually selected origin point _jc_save_fromStation and its value, as well as the departure date _jc_save_fromData and destination _jc_save_toStation . Each place name will correspond to an exclusive value, so if you are to brush tickets from Beijing to Shanghai, you need to manually check Beijing and Shanghai respectively corresponding value value, and recorded, later useful.

Next, execute the following instructions to add the cookie information:

b.cookies.add({"_jc_save_fromDate":"2016-01-18"# 此处添加出发日期b.cookies.add({"_jc_save_fromStation":"此处添加各地名所对应的value值"# 此处添加出发地b.cookies.add({u‘_jc_save_toStation‘:‘此处添加各地名所对应的value值‘# 此处添加目的地

After that, when you refresh the page again, you can see that the browser will automatically fill in the previously filled content, which saves a lot of time when the swipe ticket is executed:

The subsequent operation is not much, Reload page, and then look for the page "Query" button and click, and then find the clickable "booking" button, here is a problem is there may be multiple shifts, so if you want to manipulate the code to click on a shift "booking" button, it is not difficult, You can use the following directives:

b.find_by_text(u"查询").click()b.find_by_text(u"预订")[1# 下标1表示买的是可选班次的第二班车

The final execution of a few instructions automatically select the ticket buyers, and then manually select the verification code picture, you can complete the ticket (verification code or an insurmountable gap).

This is the entire process, in the complete code, just add some basic loops and judgments, all of which add up to less than 100 lines. The whole process of manually entering the verification code is unavoidable, unless the database with the verification code, and then on this basis to do some image recognition algorithm, but this will be a heavy task.

Reference Link: http://www.jb51.net/article/75992.htm

Use Python and splinter to achieve 12306 train ticket enquiries and robbery tickets

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.