What is the 12306 of the abnormal verification code? I have a python artifact!

Source: Internet
Author: User
Tags ticket

Preface: Everybody read with me, Python big fa good, follow this baby to rob 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:windowssystem32 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

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

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"

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

123 button = b. Find_by_value(u"Baidu") or button = b . find_by_id(u"su")

12 How do i click it? Simply the button below. 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"

1 b. Is_text_present("Splinter.cobrateam.info")

Returns true if the page exists, and vice versa

How to quit?

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

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

12345 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

1 b. Cookies. All()

1234 {u' Bigipserverotn ': u' 1977155850.38945.0000 ', u 'jsessionid ': u' 0a01d97598f459f751c4ae8518dbfb300da7001b67 ', u '__nrf ': u' 95d48fc2e0e15920bfb61c7a330ff2ae ', u' current_captcha_type ': u' Z '}

123456789101112 And then we need to add the departure point, this has to be checked, it's simple.URL Encryptionb. Cookies. Add({"_jc_save_fromstation":"%u4e0a%u6d77%2cshh"}) Add a departure dateb. Cookies. Add({"_jc_save_fromdate":"2016-01-20"}) Add destinationb. Cookies. Add({u' _jc_save_tostation ':'%u6c38%u5dde%2caoq '}) Note: If it is modified, it is calledThe Add method, if the passed-in dictionary key value already exists, replaces theFor example, change the destination to another locationxxxx, as below canb. Cookies. Add({u' _jc_save_tostation ':' xxxxxx '})

And look at the current cookie value.

1 b. Cookies. All()

1234567 {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.

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

1 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 ~ ~

QQ group 290551701 gathers a lot of Internet elite, technical director, architect, project Manager! Open source technology research, Welcome to the industry, Daniel and beginners are interested in engaging in IT industry personnel to enter!

What is the 12306 of the abnormal verification code? I have a python artifact!

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.