#-*-Coding:utf-8-*-from splinter.browser import browserfrom time Import sleepimport traceback# Initialization information # username, password username = U "username" passwd = u "Password" # cookies are worth looking for yourself starts = u "Hangzhou, hzh" ends = u "Yellowstone, HSN" # time format 2016-03-22dtime = u "2016-03-22" # n°, select the number of trips, 0 from the top Below Click order = 0## #乘客名, if it is their own, then there is a link is their own, will jump to the personal center, with Index[1] #如果乘客不是自己, then Index[0]pa = u "Name" "" "" "" " ://kyfw.12306.cn/otn/leftticket/init "Login_url =" https://kyfw.12306.cn/otn/login/init "Initmy_url =" https:// kyfw.12306.cn/otn/index/initmy12306 "Def login (): #点击登录, from the ticketing page to the login page b.find_by_text (u" Login "). Click () Sleep (3) #给n Ame Loginuserdto.user_name Enclose the user name #也可以用Id查找的方法b. find_by_id (' username '). Fill (username) b.fill ("Loginuserdto.user_ Name ", username) sleep (1) #给name为userDTO. Password value b.fill (" Userdto.password ", passwd) sleep (1) #验证码必须手动输入 Print U "Wait for verification code, self-input ..." #检测页面是否跳转 (whether the user entered a verification code and commits) while true:if b.url! = Initmy_url:sleep (1) Else:breakdef Huoche (): Globalb b = Browser (driver_name= "Chrome") #访问12306 b.visit (Ticket_url) #查找带登录的字符串, U is to avoid Chinese error while B.is_text_presen T (U "login"): Sleep (1) #如果存在调用登录方法 login () if B.url = = Initmy_url:break #当执行完lo After the Gin method, the login succeeds and continues down the try:print u "buy Ticket page ..." # Jump Repurchase Ticket page b.visit (ticket_url) # Load Query information B.coo Kies.add ({"_jc_save_fromstation": Starts}) B.cookies.add ({"_jc_save_tostation": Ends}) b.cookies.add ({"_jc_s Ave_fromdate ": Dtime}) B.reload () sleep (2) count = 0 # Loop Click to book #order =0 the First Bus starts all bookings If order! = 0: #如果页面没有跳转到, stating no grab tickets, continue to rob while b.url = = Ticket_url: #点击查询进行刷新 B.find_by_text (U "Query"). Click () Count +=1 print U "loop click Query ... %s Times "% Count sleep (1) Try: #点击预订的那班车 B.find_by_text (U "Reservation") [Order-1].click () excepT:print U "not yet started booking" continue else:while B.url = = Ticket_url: B.find_by_text (U "Query"). Click () Count + = 1 Print u "loop click Query ... %s Times "% Count sleep (1) Try: #从第一个开始一次预订 for I in B.F Ind_by_text (U "Reservation"): I.click () except:print U "not yet scheduled" Continue sleep (1) #若循环结束了, select Contact B.find_by_text (PA) [0].click () Print U "What you can do ..... No more action on the browser "except Exception as E:print (Traceback.print_exc ()) If __name__ = =" __main__ ": Huoche ()
Python Auto-grab tickets