forked from Wars/getXM9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetxiaomi9.py
83 lines (67 loc) · 2.82 KB
/
getxiaomi9.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import time
from selenium import webdriver
class xiaomi():
lurl = 'https://account.xiaomi.com/'
burl = 'https://item.mi.com/product/10000134.html'
atime = 1552960800
# 该方法用来确认元素是否存在,如果存在返回flag=true,否则返回false
def isElementExist(self, element):
flag = True
browser = self.b
try:
browser.find_element_by_css_selector(element)
return flag
except:
flag = False
return flag
def __init__(self, usernme, pwd):
self.username = usernme
self.pwd = pwd
self.b = webdriver.Firefox(executable_path='./geckodriver')
def login(self):
print('正在登陆...\n')
self.b.get(self.lurl)
self.b.find_element_by_id('username').send_keys(self.username)
self.b.find_element_by_id('pwd').send_keys(self.pwd)
self.b.find_element_by_id('login-button').click()
def check_phone(self):
time.sleep(1.5)
sreach_window = self.b.current_window_handle
if self.isElementExist('.btn_tip.btn_commom.verify-sendbtn'):
self.b.find_element_by_css_selector('.btn_tip.btn_commom.verify-sendbtn').click()
sms_code = input('code:\n')
self.b.find_element_by_name("ticket").send_keys(sms_code)
self.b.find_element_by_css_selector('.btn_tip.btn_commom.btn-submit').click()
def choose_type(self):
self.b.find_element_by_xpath('//*[@id="J_list"]/div[1]/ul/li[2]').click()
self.b.find_element_by_xpath('//*[@id="J_list"]/div[2]/ul/li[2]').click()
self.b.find_element_by_xpath('//*[@id="J_list"]/div[3]/ul/li[1]').click()
def buy(self):
print('进入米9秒杀页面...\n')
self.b.get(self.burl)
try:
self.b.find_element_by_xpath('//*[@id="J_buyBox"]/div/div[1]/div/a[1]').click()
self.b.find_element_by_xpath('//*[@id="J_agreeModal"]/div[3]/div/button[2]').click()
time.sleep(3)
self.b.find_element_by_xpath('//*[@id="J_buyBtnBox"]/li[1]').click()
except:
pass
print('正在选择型号配件...\n')
self.choose_type()
while True:
if time.time() > self.atime:
try:
self.b.find_element_by_xpath('//*[@id="J_buyBtnBox"]/li[1]').click()
print('开始抢购...\n')
except:
print('异常,退出抢购')
break
else:
print('\n\n', '还未到达抢购时间\n', '当前时间是:', time.time(), '\n', '抢购时间是:', self.atime, '\n\n')
if __name__ == '__main__':
username = input('username:\n')
pwd = input('pwd:\n')
a = xiaomi(usernme=username, pwd=pwd)
a.login()
a.check_phone()
a.buy()