Grab a gun with python. New Year's train ticket

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 .

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 Browser b = 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.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/76/F5/wKioL1ZfvQDT-UqpAAB42MSWUfo592.png "title=" Image201512011.png "alt=" Wkiol1zfvqdt-uqpaab42mswufo592.png "/>

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 splinterin the input box, and of course you can type in Chinese , but it is best to specify Unicode encoding, such as u " i "

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 the IDof these elements,value or something, through Chrome 's F12 Find what you need.

Well, let's find out the click Bar.

we found that Baidu search bar of the 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 Button.Click ()

What's the use of this?

Let's find out if there's anything I want to find on the page, like looking for an 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 Browser b = 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 look at our cookies , of course, there's no departure date .

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

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/76/F5/wKioL1ZfvYLQ6MkGAAEsWAESOls486.png "title=" Image201512013.png "alt=" Wkiol1zfvylq6mkgaaeswaesols486.png "/>

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 a simple URL encryption B.cookies.add ({"_jc_save_fromstation": "%u4e0a%u6d77%2cshh"}) Add the 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 modified, the Add method is called, if the passed dictionary key value already exists, replace for example, Change the destination to another place XXXX, as below can be 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.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/76/F5/wKioL1ZfveHSfWCjAADq2GKCOMQ635.png "title=" Image201512012.png "alt=" Wkiol1zfvehsfwcjaadq2gkcomq635.png "/>

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, indexed values naturally in It's 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.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/76/F5/wKioL1ZfvfGTYc4pAAD49OWsYig532.png "title=" Image201512031.png "alt=" Wkiol1zfvfgtyc4paad49owsyig532.png "/>

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

Then the first method is basically explained.

The above steps are stacked together in fact , but not in the line.

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

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/76/F5/wKioL1Zfvi_je3LwAAAsBXlJ44s184.png "title=" Picture 1111.png "alt=" Wkiol1zfvi_je3lwaaasbxlj44s184.png "/>

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, leave the mailbox bar ~ ~ ~


Finally hope that everyone can buy a fire ticket home for the New Year ~ ~ ~

This article is from the "Ear Notebook" blog, please make sure to keep this source http://youerning.blog.51cto.com/10513771/1719159

Grab a gun with python. New Year's train ticket

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.