How to write a simple 12306 grab ticket software in Python

Source: Internet
Author: User
Tags ticket

The so-called Rob is actually at the beginning of the ticket to make the first request and click on the reservation. As programmers, we can completely let the program do it. It took me a couple of hours to write a demo. The tool sets used are: Python3.6, Selenium, Chromdriver. The program itself is the process of things, there is nothing to introduce. The code is as follows

#Coding=utf-8 fromSeleniumImportWebdriver fromTimeImportSleepImportTracebackticket_uri='Https://kyfw.12306.cn/otn/leftTicket/init'Login_uri='Https://kyfw.12306.cn/otn/login/init'My_uri='https://kyfw.12306.cn/otn/index/initMy12306'LOGIN= u'Login' fromSplinter.browserImportBrowser fromTimeImportSleepImportTracebackticket_uri='Https://kyfw.12306.cn/otn/leftTicket/init'Login_uri='Https://kyfw.12306.cn/otn/login/init'My_uri='https://kyfw.12306.cn/otn/index/initMy12306'LOGIN='Login_user'deflogin (): brw.find_element_by_id (Login). Click () Sleep (3) uname=' 123456789@qq. com'pwd=' xxxyyyzzz'brw.find_element_by_id ('username'). Send_keys (uname) sleep (1) brw.find_element_by_id ('Password'). Send_keys (PWD) sleep (1)     whileTrue:ifBrw.current_url! =My_uri:sleep (1)        Else:             BreakdefAddcookie (cklist): Li=list () forDinchcklist:ifd['name'] =='_jc_save_tostation' ord['name'] =='_jc_save_todate' ord['name'] =='_jc_save_fromstation': Li.append (d)returnLidefBook ():GlobalBRW BRW=Webdriver. Chrome () brw.set_window_size (1366, 768) Brw.get (Ticket_uri) sleep (3)     whilebrw.find_element_by_id (login): Login ()ifBrw.current_url = =My_uri: Break; Try: Brw.get (Ticket_uri) sleep (2)        #set srcBRW.FIND_ELEMENT_BY_ID ('Fromstationtext'). Clear () brw.find_element_by_id ('Fromstationtext'). Click () brw.find_element_by_id ('Fromstationtext'). Send_keys (U'Hefei South') Sleep (3)        #Set DSTBRW.FIND_ELEMENT_BY_ID ('Tostationtext'). Clear () brw.find_element_by_id ('Tostationtext'). Click () brw.find_element_by_id ('Tostationtext'). Send_keys (U'Wuhan') Sleep (3)        #Set left Date        Print('Please click Train Date') Sleep (5) Cke=brw.get_cookies () Li=Addcookie (CKE) forXinchLi:brw.add_cookie (x) Brw.refresh () Count=0 Success=Falseif  notSuccess: whileBrw.current_url = =TICKET_URI:brw.find_element_by_id ('Query_ticket'). Click () Sleep (2)                Print(U'First%d refreshes'%count) Count+ = 1Brw.find_element_by_partial_link_text ('D3057')    exceptException as E:Print(Traceback.print_exc ())if __name__=="__main__": Book ()

How to write a simple 12306 grab ticket software in Python

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.