python reptile learning simple little program in Python grab a ticket

Making use of Python automatically grab tickets applets, New Year never again worry no ticket!

Foreword

Every Chinese New Year because many people will not grab tickets but can not go home, so small make use of a tool written in Python automatically grab a ticket and hope that we can grab a train, go home have a good year!

I wanted to write a practice your hand, but the instant I thought, what Python itself the biggest advantage is that there are not that many people have made good Niubi wheels yet? You just need to know the wheels and will be used on the line, this will save you a lot of energy and time, and standing on the shoulders of giants, can see farther. So I search on github, and sure enough there are a lot of programs to grab votes, some Python2, some Python3, start by sorting data, after personally use and comparison, I chose a relatively good use of the program, and a little in order to improve and refine .

Man of few words said, directly on the code:

  1  '' ' 
  2  What I do not know how you can add in the learning process
   3  Python learning exchanges buttoned Qun, 934 109 170
   4  the group has a good learning video tutorials, development tools and e-books.
  5  Share python current business needs and your talent and how good python learning from zero base, and learn what content.
  . 6  '' ' 
  . 7   
  . 8 @@ -0,0 +1,124 @@
   . 9  # - * - Coding: UTF-. 8 - * - 
10  "" " 
. 11  @author: liuyw
 12 is  " "" 
13 is  from splinter.browser Import Browser
 14  from Time Import SLEEP
 15  Import the traceback
 16  Import Time, SYS
. 17   
18 is  class huoche (Object):
 . 19    the driver_name = '' 
20 is    executable_path = '' 
21 is    # username, password 
22 is    username = U " XXX " 
23 is    the passwd = U " XXX " 
24    # Cookies find their worth, respectively, the following two Shenyang, Harbin, 
25    Soho starts U = " % u6C88% u9633% 2CSYT " 
26    ends U = " % u54C8% u5C14% u6EE8% 2CHBB " 
27   
28    # time format 2018-01-19 
29    DTIME = U "2018-01-19" 
30    # trips, first select several times, from below 0 Click 
31 is    Order = 0
 32    # ## passenger name 
33 is    Users = [U " XXX " , U " XXX " ]
 34 is    # # seats 
35    XB = U " second class " 
36    PZ = U " adult tickets " 
37 [   
38 is    "", " url " "" 
39    ticket_url = " https://kyfw.12306.cn/otn/leftTicket/init " 
40    LOGIN_URL = "https://kyfw.12306.cn/otn/login/init"
 41   initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306"
 42   buy = "https://kyfw.12306.cn/otn/confirmPassenger/initDc"
 43  
 44   def __init__(self):
 45     self.driver_name = 'chrome'
 46     self.executable_path = 'D:/chromedriver'
 47  
 48   def login(self):
 49     self.driver.visit(self.login_url)
 50     self.driver.fill("loginUserDTO.user_name", self.username)
 51     # sleep(1)
 52     self.driver.fill("userDTO.password", self.passwd)
 53     print(u"等待验证码,自行输入...")
 54     while True:
 55       if self.driver.url != self.initmy_url:
 56         sleep(1)
 57       else:
 58         break
 59  
 60   def start(self):
 61     self.driver = Browser(driver_name=self.driver_name,executable_path=self.executable_path)
 62     self.driver.driver.set_window_size(1400, 1000)
 63     self.login()
 64     # sleep(1)
 65     self.driver.visit(self.ticket_url)
 66     try:
 67       print(u"购票页面开始...")
 68       # sleep(1)
 69       # 加载查询信息
 70       self.driver.cookies.add({"_jc_save_fromStation": self.starts})
 71       self.driver.cookies.add({"_jc_save_toStation": self.ends})
 72       self.driver.cookies.add({"_jc_save_fromDate": self.dtime})
 73  
 74       self.driver.reload()
 75  
 76       count = 0
 77       if self.order != 0:
 78         while self.driver.url == self.ticket_url:
 79           self.driver.find_by_text(u"查询").click()
 80           count += 1
 81           print(u"循环点击查询... 第 %s 次" % count)
 82           # sleep(1)
 83           the try :
 84              self.driver.find_by_text (U " reservation " ) [self.order -. 1 ] .click ()
 85            the except Exception AS E:
 86              Print (E)
 87              Print (U " has not yet begun reservation " )
 88              Continue 
89        the else :
 90          the while self.driver.url == self.ticket_url:
 91 is            self.driver.find_by_text (U " query " ) .click ()
 92            COUNT +. 1 =
 93            Print (U "Click query cycle times ...% s of " % COUNT)
 94            # SLEEP (0.8) 
95            the try :
 96              for I in self.driver.find_by_text (U " reservation " ):
 97                i.click ()
 98                SLEEP (. 1 )
 99            the except Exception AS E:
 100              Print (E)
 101              Print (U " not started reservation S% " % COUNT)
 102              Continue 
103        Print (U " start Book ... ")
104       # sleep(3)
105       # self.driver.reload()
106       sleep(1)
107       print(u'开始选择用户...')
108       for user in self.users:
109         self.driver.find_by_text(user).last.click()
110  
111       print(u"提交订单...")
112       sleep(1)
113       self.driver.find_by_text(self.pz).click()
114       self.driver.find_by_id('').select(self.pz)
115       # sleep(1)
116       self.driver.find_by_text(self.xb).click()
117       sleep(1)
118       self.driver.find_by_id('submitOrder_id').click()
119       print(u"开始选座...")
120       self.driver.find_by_id('1D').last.click()
121       self.driver.find_by_id('1F').last.click()
122  
123       sleep(1.5)
124       print(u"确认选座...")
125       self.driver.find_by_id('qr_submit_id').click()
126  
127     except Exception as e:
128       print(e)
129  
130 if __name__ == '__main__':
131   huoche = huoche()
132   huoche.start()

 

Guess you like

Origin www.cnblogs.com/xiaoyiq/p/11333931.html