What is the abnormal Verification Code of 12306? I have Python artifacts !, Python 12306
Everyone should join me in reading 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 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.
12345 |
From splinter. browser import Browser B = 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
Name
With the content specified
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"
1 |
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.
123 |
Button = B. find_by_value (u "Baidu") or button = B. find_by_id (u "su ") |
12 |
How can I click it? Simple 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"
1 |
B. is_text_present ("splinter.cobrateam.info ") |
If the page exists, True is returned, and vice versa.
How to exit?
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 ..
123456789 |
From splinter. browser import Browser B = Browser (driver_name = "chrome") url = "https://kyf42412306.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.
12345 |
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:
1234 |
{U 'biginserverotn': u'100. 38945.0000 ', u'jsessionid': u'hangzhou', U' _ NRF': u'95d48fc2e0e15920bfb61c7a330ff2ae ', u'current _ captcha_type': u 'Z '} |
123456789101112 |
Then we need to add the sender location. We have to check it by ourselves. It is simple url encryption B. cookies. add ({"_ jc_save_fromStation": "% u4E0A % u6D77 % 2 CSHH"}) 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 it with, for example, change the destination to xxxx elsewhere, as shown below. cookies. add ({U' _ jc_save_toStation ': 'xxxxxx '}) |
Check the current cookies.
1234567 |
{U 'biginserverotn': u'100. 38945.0000 ', u'jsessionid': u'hangzhou', U' _ NRF': u'95d48fc2e0e15920bfb61c7a330ff2ae ', U' _ jc_save_fromDate ', u' _ bucket': U' % u4E0A % u6D77 % 2CSHH ', U' _ jc_save_toStation': U' % u6C38 % u5DDE % 2CAOQ ', u'current _ captcha_type ': u'z '} |
Now that cookies are ready, reload them and start querying.
12 |
B. reload () B. find_by_text (u ""). click () |
Was it found that all the locations and dates were filled in?
In this step, we have to confirm that we have logged on. If everything is ready, you can get a ticket.
It is worth noting that the above steps can be completed manually.
Next we need to use the combination BeautifulSoup to determine whether the reservation can be ordered. Of course, you can also choose a single point.
I just want to buy a high-speed train. In this case, the following two methods are available: one click, one click, and many times, without any chance.
Let's start with the first method. This method is relatively simple and does not need to be used in other libraries. splinter alone is enough. Let's start with a simple one.
From the perspective of my own train lines, there are a total of six trips. I just want to make a high-speed train. Is it enough for the reservation that I keep making for the high-speed train? Of course !!
There are six reservations in total. My reservation is in the second place, and the index value is naturally in the first place. (Why am I not asked for python !!!)
B. find_by_text (u "") [1]. click ()
Then, if the reservation is successful
Should jump to the location of the selected passenger,
We need to select passengers as needed.
1 |
B. find_by_text (u "Your name") [1]. click () |
Then the first method is basically explained.
In fact, the above steps are only 100 lines.
Then there should be a third method, that is, using the default automatic query, the default is to refresh once every 5 seconds, but we all know that the 5 seconds interval during the Spring Festival is too long, what should we do?
Chrome F12: Click Console
Input autoSearchTime = xxx
The default value is in milliseconds. That is to say, the default value is 5000 ms, but do not modify it too small. It will be detected and the network is busy !!! I changed to ms, and it seems that the network is busy after 10 to 15 times.
In fact, the purpose of using Python to brush votes is also to suspend the process of refreshing the page targeted to grab votes, so we don't have to brush it all the time. As for the source code, the drivers are all linked below ~~ There are still many shortcomings in the Code and the writing is not elegant enough. You can refer to it and modify it as needed ~ The emails left should be sent out, but the emails cannot be sent one by one ~~
QQ Group 290551701 has gathered many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!