Use Python and Splinter to query and grab tickets for train tickets of 12306

Source: Internet
Author: User

Use Python and Splinter to query and grab tickets for train tickets of 12306

I haven't used Python for a while. A friend mentioned an article about how to use Python to get a train ticket a few days ago. Baidu has technical details on how to get a train ticket, but there are a lot of information on the Internet, it's not new. After learning about some technical means and reading some blog posts from some great gods, I also tried to implement it. The code is rough and entertaining. This article is completed in Windows. It should be noted that the verification code Part in the ticket grabbing process can only be manually completed.

First, I need the following tools and components:

Chrome browser driver ChromeDriver Python 3.5 Web application testing tool Splinter

Chrome can be downloaded on its own, its browser drive ChromeDriver can be found in the following link: http://chromedriver.storage.googleapis.com/index.html? Route = 2.20/

Download Python from the official website, select Windows x86 executable installer, and install IDLE and pip together during the installation process. You can check the option to add Python as the environment variable (run Python in Windows cmd, environment variables must be set ). After the installation is complete, run pip install splinter to install Splinter.

The basic configuration has been completed. Test it. Run Python in cmd and enter the following commands:

>>> from splinter.browser import Browser>>> b = Browser(driver_name="chrome")>>> b.visit("http://www.qq.com")

You can create a basic file and write the preceding three statements:

Google Chrome developer tools are required for subsequent operations. Press "F12" or "shift + ctrl + I" to call out. Find the Elements tab, where the information of each element on the current page is recorded, such as the following search bar and the "Baidu click" button, you can select the page element or location you want to view, elements will jump to the corresponding implementation code.

Name = "wd" in the search bar, "Baidu click" Key id = "su". After obtaining this information, you can perform the following three steps:
1. input:b.fill("wd", "splinter")You can search for splinter in the search box.
2. input:Button = B. find_by_value (u "Baidu ")Find this button
3. input:button.click()Click this button

For more operations, refer to the official splinter documentation...

Start To Go To The 12306 URL for the operation:

Similar to operations on the Baidu page, enter personal information in the username and password column:

B. find_by_text (u "login "). click () B. fill ("loginUserDTO. user_name ", username) # enter your account B in username. fill ("userDTO. password ", passwd) # enter the account password in the passwd Section

The reason is that the ticket is semi-automated because some information needs to be collected manually before the ticket is snatched, such as manually selecting a departure location, date, and arrival location. Then, F12 opens the developer tool and clicks Resources, find the cookies option and you will see the following content:

You can see through cookiesManually selectDeparture location_jc_save_fromStationAnd its value, as well as the departure date_jc_save_fromDataAnd destination_jc_save_toStation. Each place name corresponds to an exclusive value. Therefore, if you want to purchase a ticket from Beijing to Shanghai, you need to manually check the value corresponding to Beijing and Shanghai and record it, later.

Next, execute the following command to add cookies:

B. cookies. add ({"_ jc_save_fromDate": "2016-01-18"}) # add departure date B here. cookies. add ({"_ jc_save_fromStation": "add the value corresponding to the names of different regions"}) # add the departure location B here. cookies. add ({U' _ jc_save_toStation ': 'add the value corresponding to each region name'}) # add a destination here

Then, when you refresh the page again, you can find that the browser automatically fills in the previously manually filled content. This will undoubtedly save a lot of time when executing the ticket flushing:

There are not many subsequent operations. reload the page, search for the "query" button in the page, and click again.ClickableThe "Reservation" button indicates that multiple flights may occur. Therefore, it is not difficult to click the "Reservation" button for a certain shift by using the code. Use the following command:

B. find_by_text (u "query "). click () B. find_by_text (u "Reservation") [1]. click () # subscript 1 indicates that the second shuttle bus is available for optional flights.

Database, and then make some image recognition algorithms on this basis, but this is a heavy workload task.

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.