Use Python to grab the New Year's train ticket with source code

Source: Internet
Author: User
Preface: Everybody read with me, Python dafa good, follow this baby to rob the train ticket with Python

First, we need splinter.

Installation:

Pip install splinter-i http://pypi.douban.com/simple--trusted-host pypi.douban.com

Then you need a browser driver, of course, with chrome.

Download Address:

http://chromedriver.storage.googleapis.com/index.html?path=2.20/

According to the download of your own computer system select Download package, my Windows will use Win32

After extracting it directly into the C:\Windows\System32 directory, you can, of course, get an environment variable for the driver.

Note: I am under the driver version is 2.19, according to their own needs to download the corresponding version, my 2.20 version has an error

First, a simple test, recommended Ipython instead of Python's own interactive interface

From splinter.browser Import browserb = browser (driver_name= "Chrome") b.visit ("http://www.baidu.com") # # #注意不要去掉http:/ /

Then kind a bit, with Baidu search some things. Like Splinter.
In the above we have opened Baidu's Web site

And then we'll type something like a search.

From above can be found, the input box of the NAME=WD, through fill seems to only be filled by name

Official Note: Fill the field identified by "name" with the content specified by ' value '.

Then search for splinter in the input box, and of course you can type in Chinese, but it is best to specify Unicode encoding, such as U "I"

Copy the Code code as follows:


B.fill ("WD", "Splinter")

Interesting things, you will find that you do not need to click on the "Baidu" on the search page

However, if multiple searches, we still need to click on "Baidu a Bit"

The following is not taking everyone to find these elements of the id,value or something, through the chrome F12 find what you need it

Well, let's find out the click Bar.

We found that Baidu search bar value= "Baidu a Bit", id= "su"

So pull this button out.

button = B.find_by_value (U "Baidu") or button = b.find_by_id (U "su")

How do i click it? Simple as follows

Copy the Code code as follows:


Button.Click ()


What's the use of this?

Let's find out if there is anything I want to find in the page, like looking for the address "splinter.cobrateam.info"

B.is_text_present ("Splinter.cobrateam.info")

Returns true if the page exists, and vice versa

How to quit?

B.quit ()

Well, here's a simple introductory tutorial on the official documentation, so let's get to the bottom of the story.
The individual is to give people to fish than to give people to fishing, so I try to explain all the content, rather than send a code, let everyone copy, and then superficial understanding.
It is worth noting that I will not say what how to crack the verification code and what loopholes can be exploited what, rob the ticket to know, a little bit faster, and I want to do is to be able to do the machine to machine to do, such as click, Query, select, etc., so hope will be in or bypass this article. The author level is just a little bit.

First, let's explain the idea with Ipython.

The beginning of course is imported.

From splinter.browser Import browserb = browser (driver_name= "chrome") URL = "Https://kyfw.12306.cn/otn/leftTicket/init "B = Browser (driver_name=" Chrome ") b.visit (URL)

The first step to manually login, can fill the form by the following code, but I do not jump verification code, temporarily do not have the energy to study that thing, many forgive me, so still manually select the verification code.

B.find_by_text (U "Login"). Click () B.fill ("Loginuserdto.user_name", "xxxx") B.fill ("Userdto.password", "xxxx")

The second part chooses the departure place date and so on

Choose your departure location, date and destination via cookies

First of all, look at our cookies, of course not. Departure date or something.

As for your origin and destination of the cookie value, you have to copy it by yourself, I can not help

How do you get these values?

First, place the date in a query, then Chrome press F12 to find this part.

Open the browser to jump to this page of course there is no information we need, such as the following

B.cookies.all () {u ' bigipserverotn ': U ' 1977155850.38945.0000 ', U ' jsessionid ': U ' 0a01d97598f459f751c4ae8518dbfb300da7001b67 ', U ' __NRF ': U ' 95d48fc2e0e15920bfb61c7a330ff2ae ', U ' Current_captcha_ Type ': U ' Z '}

Then we need to add the departure point, this has to be checked by itself, is simple URL encryption

B.cookies.add ({"_jc_save_fromstation": "%u4e0a%u6d77%2cshh"})

Add a departure date

B.cookies.add ({"_jc_save_fromdate": "2016-01-20"})

Add destination

B.cookies.add ({u ' _jc_save_tostation ': '%u6c38%u5dde%2caoq '})

Note: If it is a modification, call the Add method, or replace if the passed Dictionary key value already exists

For example, to change the destination to another place XXXX, as follows

B.cookies.add ({u ' _jc_save_tostation ': ' xxxxxx '})

And look at the current cookie value.

B.cookies.all () {u ' bigipserverotn ': U ' 1977155850.38945.0000 ', U ' jsessionid ': U ' 0a01d97598f459f751c4ae8518dbfb300da7001b67 ', U ' __NRF ': U ' 95d48fc2e0e15920bfb61c7a330ff2ae ', U ' _jc_save_ FromDate ': U ' 2016-01-20 ', U ' _jc_save_fromstation ': U '%u4e0a%u6d77%2cshh ', U ' _jc_save_tostation ': U '%u6c38%u5dde% 2CAOQ ', U ' current_captcha_type ': U ' Z '}

Now that the cookies are ready, reload, start inquiring.

B.reload () B.find_by_text (U "Query"). Click ()

is not found, the location date is filled in, very cool there are wood

In this step we have to confirm that we have logged in. When everything is ready, you can brush the tickets.
It is important to note that the above steps can be done by hand.

Below we need to use the combination of BeautifulSoup to determine the booking can point, of course, you may also choose a single point of a trip

Anyway, I just want to buy high-speed rail, since this, the following two ways, one is a single trip, but the round to follow a lot of times, do not miss any chance.

First of all, this is relatively simple, do not need to use other libraries, alone splinter is enough, just start from the simple.


From my own train line to see, altogether six times, I just want to do high-speed rail, then I always point to the high-speed railway booking is not enough, of course!!
A total of six bookings, my booking in the second, the index value naturally in 1. (Python will not ask me why!!!) )

B.find_by_text (U "Reservation") [1].click ()

Then if the booking succeeds

Should jump to the location of the selected passenger,

We need to select passengers on demand.

B.find_by_text (U "Your Name") [1].click ()

Then the first method is basically explained.
The above steps stack together in fact, but 100 lines.
Then there should be a third method, that is, using the default automatic query, the default is 5 seconds to refresh once, but we all know that the Spring Festival period of 5 seconds is too long, how to do?

Chrome's F12, click Console

Input autosearchtime=xxx

The default is milliseconds, that is, the default 5000ms, but do not modify too small, will be detected and then reported that the network busy!!! I changed to 1000ms seems to be able to brush ten to 15 times to report the network busy.

In fact, with Python to brush the ticket is also to, did not Rob, the refresh page orientation Rob ticket process hangs, we do not always go to brush, as for the source code, leaving the driver are in the following link ~ ~ Codes There are many shortcomings, and write not elegant, we can refer to, according to the actual situation casually modified ~ Leave the mailbox should be sent out, a hair really do not come over ~ ~

Need source of friends can click to download: Http://pan.baidu.com/s/1gdTu7cR

  • 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.