The annual Spring Festival travel rush is coming again.

I wrote my own ticket grab script this year.

Python +splinter automatically flusher tickets, you can successfully grab tickets (depending on your network environment, and machine quality),

But it doesn’t feel perfect,

There is a god please guide perfect (or there is no other good point of thought)

Appreciate receiving

# -*- coding: utf-8 -*- """ @author: liuyw """ from splinter.browser import Browser from time import sleep import traceback import time, sys class huoche(object): """docstring for Huoche """ driver_name='' executable_path='" Password username = u"[email protected]" passwd = u" XXXX "# cookies Ends = u"%u4E0A%u6D77%2CSHH" ends = U "%u592A%u539F%2CTYV" Order = 0 ### passenger name users = [u" XXX ",u" XXX "] ## seat xb =u" second class seat "pz=u" adult ticket" """ url """ 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" buy="https://kyfw.12306.cn/otn/confirmPassenger/initDc" login_url='https://kyfw.12306.cn/otn/login/init' def __init__(self): self.driver_name='chrome' self.executable_path='/usr/local/bin/chromedriver' def login(self): self.driver.visit(self.login_url) self.driver.fill("loginUserDTO.user_name", Self.username) # sleep(1) self.driver.fill(" userto.password ", self.passwd) print u" while True: if self.driver.url ! = self.initmy_url: sleep(1) else: break def start(self): self.driver=Browser(driver_name=self.driver_name,executable_path=self.executable_path) self.driver.driver.set_window_size(1400, 1000) self.login() # sleep(1) self.driver.visit(self.ticket_url) try: Print u" Ticket page open..." 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.reload() count=0 if self.order! =0: while self.driver.url==self.ticket_url: self.driver.find_by_text(u" query ").click() count += 1 print u" Self.driver.find_by_text (u" order ")[self.order-1].click() except Exception as e: Print e print u" Continue else: while self.driver.url == self.ticket_url: Self.driver.find_by_text (u" query ").click() count += 1 print u" %s "% count # sleep(0.8) try: for I in self.driver. Find_by_text (u" 下 载 "): i.lick () sleep(1) except Exception as e: Print e print u" %s" %count Continue print u" %s" Reload (3) # self.driver.reload() print u' 'for user in self.users: self.driver.find_by_text(user).last.click() print u"..." sleep(1) # self.driver.find_by_text(self.pz).click() # self.driver.find_by_id('').select(self.pz) # # sleep(1) # Self.driver.find_by_text (self.xb).click() # sleep(1) self.driver.find_by_id('submitOrder_id').click() # print"  # self.driver.find_by_id('1F').last.click() print u" " self.driver.find_by_id('qr_submit_id').click() except Exception as e: print e if __name__ == '__main__': huoche=huoche() huoche.start()Copy the code