Python Auto-grab tickets

Source: Internet
Author: User
Tags ticket

The Dragon Boat Festival is now a holiday, but also the time to rob the ticket, using the Python program to achieve automatic ticket.

Splinter is a Python library for automated test network applications. With Splinter, you can automate everything from opening a browser, entering a URL, filling out a form, clicking a button, and more. Therefore, we need to introduce this library. Install via PIP install Splinter.

1 #-*-coding:utf-8-*-2 """3 @author: Wang Linjie4 """5 6  fromSplinter.browserImportBrowser7  fromTimeImportSleep8 ImportTraceback9 Ten  One  A #user name, password -Username = uthe user name" -passwd = uthe password" the #cookies are worth looking for themselves, the following two are Beijing, Anyang -Starts = u"%U5317%U4EAC%2CBJP" -Ends = u"%u5b89%u9633%2cayf" - #Time Format 2016-01-31 +Dtime = u"2018-06-15" - #Train, select the number of times, 0 then click +Order =0 A ## #乘客名 atPA = u"Wang Lin Jay" -  - """website""" -Ticket_url ="Https://kyfw.12306.cn/otn/leftTicket/init" -Login_url ="Https://kyfw.12306.cn/otn/login/init" -Initmy_url ="https://kyfw.12306.cn/otn/index/initMy12306" in  -  to deflogin (): +B.find_by_text (U"Login"). Click () -Sleep (3) theB.fill ("Loginuserdto.user_name", username) *Sleep (1) $B.fill ("Userdto.password", passwd)Panax NotoginsengSleep (1) -     PrintU"wait for verification code, self-input ..." the      whileTrue: +         ifB.url! =Initmy_url: ASleep (1) the         Else: +              Break -  $ defHuoche (): $     Globalb -b = Browser (driver_name="Chrome") - b.visit (Ticket_url) the  -      whileB.is_text_present (U"Login"):WuyiSleep (1) the Login () -         ifB.url = =Initmy_url: Wu              Break -  About     Try: $         PrintU"ticket Purchase page ..." -         #Skip Repurchase Ticket page - b.visit (Ticket_url) -  A         #Load Query Information +B.cookies.add ({"_jc_save_fromstation": Starts}) theB.cookies.add ({"_jc_save_tostation": Ends}) -B.cookies.add ({"_jc_save_fromdate": Dtime}) $ b.reload () the  theSleep (2) the  theCount =0 -         #Circular Click to book in         ifOrder! =0: the              whileB.url = =Ticket_url: theB.find_by_text (U"Enquiry"). Click () AboutCount +=1 the                 PrintU"loop-click Query ... %s Times"%Count theSleep (1) the                 Try: +B.find_by_text (U"Booking") [Order-1].click () -                 except: the                     PrintU"haven't started booking yet"Bayi                     Continue the         Else: the              whileB.url = =Ticket_url: -B.find_by_text (U"Enquiry"). Click () -Count + = 1 the                 PrintU"loop-click Query ... %s Times"%Count theSleep (1) the                 Try: the                      forIinchB.find_by_text (U"Booking"): - I.click () the                 except: the                     PrintU"haven't started booking yet" the                     Continue94Sleep (1) theB.find_by_text (PA) [1].click () the         PrintU"All that can be done .....  No more action on the browser" the     exceptException as E:98         Print(Traceback.print_exc ()) About  - if __name__=="__main__":101Huoche ()

Tips:

How do I get the cookie value from the start station and terminal in Chrome?

Log in first 12306, enter a location date or something, and then press F12 in the Chrome browser to see the following page and find the corresponding value in the resource option.

Cookie values in the corresponding table:

The value of _jc_save_fromestation is the origin

The value of _jc_save_tosatation is the destination

_jc_save_fromdate Departure Date

_jc_save_todate return Date

Python Auto-grab tickets

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.