1 #!/bin/env python
2 # coding=utf-8
3
4 from selenium import webdriver
5 import time 6 import unittest 7 8 9 class GetXiaoMi: 10 driver = webdriver.Chrome() 11 url = 'https://item.mi.com/product/10000041.html' 12 13 def login(self): 14 # try: 15 self.driver.get(self.url) 16 home_login_butten = self.driver.find_element_by_xpath(".//*[@id='J_buyBox']/div/div[1]/div/a[1]") 17 home_login_butten.click() 18 input_username = self.driver.find_element_by_class_name('item_account') 19 input_username.clear() 20 input_username.send_keys('username') 21 input_password = self.driver.find_element_by_id('pwd') 22 input_password.clear() 23 input_password.send_keys('password') 24 login_butten = self.driver.find_element_by_id('login-button') 25 login_butten.click() 26 self.driver.assertIsNotNone(self.driver.find_element_by_xpath(".//*[@id='error-outcon']/div/span]"), 27 u"登陸失敗,請檢查用戶名或密碼") 28 # except Exception, e: 29 # print e 30 31 @staticmethod 32 def get_sys_time(): 33 sys_time = time.time() 34 return sys_time 35 36 @staticmethod 37 def set_stamp(): 38 set_time = '2017-05-12 09:59:55' # 設置搶購時間,最好提前幾秒 39 # 將其轉換為時間數組 40 time_array = time.strptime(set_time, '%Y-%m-%d %H:%M:%S') 41 # 轉換為時間戳 42 time_stamp = int(time.mktime(time_array)) 43 return time_stamp 44 45 def get_xiaomi(self): 46 try: 47 if self.get_sys_time() >= self.set_stamp(): 48 while True: 49 self.driver.find_element_by_class_name('btn btn-primary btn-biglarge J_proBuyBtn add').click() 50 if self.assertTrue(u'你來晚了', driver.title): 51 print u'又悲劇了,默默的問候小米~' 52 break 53 else: 54 print u'<-------------趕緊手動付款吧------------>' 55 else: 56 print u'時間設置錯誤' 57 except Exception, e: 58 print e 59 60 61 if __name__ == '__main__': 62 run = GetXiaoMi() 63 run.get_xiaomi()
代碼報錯?你是否安裝了selenium模塊?請自行百度~
PS:最后我想說的是,腳本調試中,不一定好用,沒有調接口來的痛快,哎,屌絲的人生就是這樣的,干什么都得靠搶。