100 lines of Python code to automatically grab train tickets (with source code), 100 lines of python

Source: Internet
Author: User

100 lines of Python code to automatically grab train tickets (with source code), 100 lines of python

Preface

The Chinese New Year is coming again. This year, you may wish to write a code to get a train ticket home. Is it Cool. I won't talk much about it below. Let's take a look at the detailed introduction.

Prepare:

  • 12306 Website user name and password
  • Chrome browser and download chromedriver
  • Download the Python code from the network. [click to download | Local download]

Code is developed using Python + Splinter. Splinter is an open-source Web application testing tool developed using Python. It can help you automatically browse websites and interact with them.

Official Splinter Website: http://splinter.readthedocs.io/en/latest /.

When Splinter is executed, the browser you specified is automatically opened to access the specified URL. Then, any simulated behavior you develop will be automatically completed. You just need to sit in front of your computer and watch the various actions on the screen like watching a movie and then collect the results.

Understanding principles:

Find the corresponding URL and find the control to simulate logon, query, and ticket booking operations. The key is to find the control name. The difficulty is that the page value is not directly entered at the beginning, and needs to be found in the cookie.

12306 query URL: https://kyfw.12306.cn/otn/leftTicket/init

12306 login URL: https://kyfw.12306.cn/otn/login/init

My 12306URL: https://kyfw.12306.cn/otn/index/initMy12306

Ticket Validation URL: https://kyfw.12306.cn/otn/confirmPassenger/initDc

Open the Python code URL and find the control fill value:

Def login (self): self. driver. visit (self. login_url) # Fill in the username self. driver. fill ("loginUserDTO. user_name ", self. username) # Fill in the password self. driver. fill ("userDTO. password ", self. passwd) print u "Wait for the verification code and enter the verification code..."

Find the user name and password control name

Locate the Start Control name

Determine the value of the starting place. In Chrome browser, the "check" function (Press F12), Network ---> Cookies are found:

The value of the starting place in the cookie.

Copy the cookie value at the beginning. I copied several common cities and put them in the dictionary:

Cities = {'chengdu ':' % u6210 % u90FD % 2CCDW ', 'chongqing': '% u91CD % u5E86 % 2ccqw', 'beijing ': '% u5317 % u4EAC % 2CBJP', 'guangzhou ':' % u5E7F % u5DDE % 2CGZQ ', 'hangzhou':' % u676D % u5DDE % 2CHZH ', 'yichang ': '% u5B9C % u660C % 2CYCN', 'zhengzhou ':' % u90D1 % u5DDE % 2CZZF ', 'shenzhen': '% u6DF1 % u5733 % 2CSZQ', 'xi'an ': '% u897F % u5B89 % 2CXAY', 'dalian ':' % u5927 % u8FDE % 2CDLT ', 'wuhan': '% u6B66 % u6C49 % 2cwhn', 'shanghai ': '% u4E0A % u6D77 % 2CSHH', 'nanjing ':' % u5357 % u4EAC % 2cnhangzhou', 'hefei ':' % u5408 % u80A5 % 2CHFH '}

Ticket query code:

Print u "ticket purchase page starts... "# Load query information self. driver. cookies. add ({"_ jc_save_fromStation": self. starts}) self. driver. cookies. add ({"_ jc_save_toStation": self. ends}) self. driver. cookies. add ({"_ jc_save_fromDate": self. dtime}) self. driver. find_by_text (u "query "). click ()

In fact, you only need to run the Code:

Python tickets. py Shanghai Guangzhou

Of course, you still need to manually click the 12306 Verification Code of all evil, just grab the ticket and confirm the payment.

Ticket grabbing in progress

Ticket snatched!

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.