Use Python to handle 12306 abnormal verification codes, python12306

Source: Internet
Author: User

Use Python to handle 12306 abnormal verification codes, python12306

When the Spring Festival is approaching, the Helper's house editor leads everyone to grab train tickets with Python!

First, we need splinter.

Installation:

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

Then a browser driver is needed. Of course chrome is used.

:

Http://chromedriver.storage.googleapis.com/index.html? Route = 2.20/

Select the downloaded package based on the downloaded computer system. For windows, win32 is used.

Decompress the package and put it in the C: WindowsSystem32 directory. Of course, you can also get an environment variable for this driver.

Note: My driver version is 2.19. If you need to download the corresponding version, an error is reported in my 2.20 version.

First, let's test it briefly. We recommend ipython to replace the built-in interactive interface of python.

From splinter. browser import Browserb = Browser (driver_name = "chrome") B. visit ("http://www.baidu.com") ### be careful not to remove http ://

Then try it and use Baidu to search for something. For example, splinter

On this page, we have opened the Baidu website.

Then let's enter something like search.

From the above we can find that the name of the input box is wd, and it seems that it can only be filled by name through fill

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

Search for splinter in the input box. Of course, you can also enter Chinese characters, but it is best to specify Unicode encoding, such as u "I"

B. fill ("wd", "splinter ")

You will find that you do not need to click "Baidu" to go to the search page.

However, if you search multiple times, you still need to click "Baidu ".

Here we will not take you to find the IDs and values of these elements. Use chrome's F12 to find what you need.

Click find in the bar.

We found that the value in the baidu search bar is "Baidu", id = "su"

So extract the button.

Copy codeThe Code is as follows:
Button = B. find_by_value (u "Baidu ")

Or

Copy codeThe Code is as follows:
Button = B. find_by_id (u "su ")

How can I click it? Simple:

Copy codeThe Code is as follows:
Button. click ()

What is the purpose?

Let's look for something on the page that I want to find, for example, whether the address "splinter.cobrateam.info" is available"

Copy codeThe Code is as follows:
B. is_text_present ("splinter.cobrateam.info ")

If the page exists, True is returned, and vice versa.

How to exit?

Copy codeThe Code is as follows:
B. quit ()

Well, the above is a simple getting started tutorial written in reference to the official document. Let's go to the topic here ~~~

I personally think it is better to teach people to fish than to teach them to fish. So I try to explain all the content, instead of sending a code, so that everyone can copy it and try not to solve it.

It is worth noting that I will not talk about how to crack the verification code and what vulnerabilities can be used. Anyone who steals the ticket knows that it is faster, what I want to do is to hand over what the machine can do to the machine, such as clicking, querying, and selecting. So I hope this article will be bypassed if necessary. The author's level is just a little bit.

First, we will use ipython to explain our ideas.

Of course, the import started ..

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 is to manually log in and fill the form with the following code. However, I can't skip the verification code, so I have no energy to study it for the time being. I am sorry, so I still need to manually select the verification code.

B. find_by_text (u "login "). click () B. fill ("loginUserDTO. user_name "," xxxx ") B. fill ("userDTO. password "," xxxx ")

Second, select the departure location and date.

Use cookies to select the departure location, date, and destination

First, look at our cookies. Of course, there is no departure date or something.

You have to copy the cookies at your departure location and destination.

Why are these values?

Enter the location date for query, and chrome press F12 to find this part.

Open the browser and 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 sender location. We have to check it by ourselves, which is simple url encryption.

b.cookies.add({"_jc_save_fromStation":"%u4E0A%u6D77%2CSHH"})

Add 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, call the add method. If the input dictionary key value already exists, replace

For example, change the destination to xxxx elsewhere, as shown below:

b.cookies.add({u'_jc_save_toStation':'xxxxxx'})

The above content introduces how to use the Python artifact to deal with the 12306 abnormal verification code. I hope this article will help you.

Articles you may be interested in:
  • Python image verification code sharing
  • 12306 Verification Code cracking ideas
  • Python Verification Code Recognition Method
  • 12306 dynamic verification code inspired by ASP. NET to implement dynamic GIF Verification Code (with source code)
  • 12306 think about the wonderful verification code C # implement the Verification Code Program
  • How to convert a 12306 dynamic verification code to a static verification code based on C #

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.