某麦网自动刷新抢票脚本

本文最后更新于 2024年3月28日 下午

演唱会的门票越来越难抢,只能上科技,详见下面代码:

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
基于python和selenium实现的某麦网自动刷新抢票脚本
用户要提前添加好个人信息和收货地址
"""

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time

# 设置抢票链接和开票时间

# URL = "https://piao.damai.cn/146290.html?spm=a2o6e.search.0.0.7e2b4d157EDtjL"# PC页面

URL = 'http://m.damai.cn/damai/perform/item.html?projectId=146290'#手机页面
# HOUR = 19
MIN = 0
USERNAME = "13112390306"

driver = webdriver.Chrome()
# 设置等待时间
wait = WebDriverWait(driver, 5)
driver.get(URL)

"""
PC端网页抢票操作
"""
def choose(seletor):
try:
# 控件可点击时才选定
choice = wait.until(EC.element_to_be_clickable((By.XPATH, seletor)))
return choice
except TimeoutException as e:
print("Time out!")
return None
except Exception:
print("Not found!")
return None

def login():
# 点击登录
login = choose('//*[@id="userLoginInfo"]/span/a[1]')
login.click()
username = choose('//*[@id="login_email"]')
username.send_keys(USERNAME)
"""
由于密码框控件被设置为不可见
先自行输入密码并记住密码
方便刷新
(也可用cookie实现)
"""
password = choose('//*[@id="login_pwd_txt"]')
try:
password.click()
password.send_keys("********")
except Exception:
print(password)
print("Password Can't click")

def buy():
# 点击价格
try:
price = None
plus = None
buybtn = None
submit = None
booker = None
select = None
confirm = None
driver.get(URL)
# 选择价格
while None == price:
# 这里选的是580票面的,如果选其他票面,修改最后的li[*]即可
price = choose('//*[@id="priceList"]/div/ul/li[3]')
price.click()
# 数量加1
while None == plus:
plus = choose('//*[@id="cartList"]/div[1]/ul/li/span[3]/a[2]')
plus.click()
# 立即抢购
while None == buybtn:
buybtn = choose('//*[@id="btnBuyNow"]')
driver.execute_script("arguments[0].scrollIntoView();", buybtn)
buybtn.click()
# 选择购票人
while None == booker:
booker = choose('/html/body/div[3]/div[3]/div[2]/div[2]/div/a')
driver.execute_script("arguments[0].scrollIntoView();", booker)
booker.click()
# 选择、确定
while None == select:
select = choose('/html/body/div[3]/div[3]/div[12]/div/div[2]/div/div[2]/div/table/tbody/tr/label/td[1]/input')
driver.execute_script("arguments[0].scrollIntoView();", select)
select.click()
while None == confirm:
confirm = choose('/html/body/div[3]/div[3]/div[12]/div/div[2]/div/p/div/a')
driver.execute_script("arguments[0].scrollIntoView();", confirm)
confirm.click()
# 提交订单
while None == submit:
submit = choose('//*[@id="orderConfirmSubmit"]')
driver.execute_script("arguments[0].scrollIntoView();", submit)
submit.click()
except Exception:
print("抢票失败,尝试重新抢票")
buy()

def test():
login()
time.sleep(30)
print("开始抢票")
buy()
print("抢票成功")


"""
移动端抢票操作
"""

def login_mobile():
"""
点击购买进入登录界面
自行输入帐号密码
"""
# 立即购买
buybtn = None
while None == buybtn:
buybtn = choose('/html/body/div[1]/div[2]/div/div[1]/div[2]/div')
driver.execute_script("arguments[0].scrollIntoView();", buybtn)
buybtn.click()
# 默认已经选好时间了,再点击立即购买
buy = None
while None == buy:
buy = choose('/html/body/div[1]/div[3]/div[2]/div[1]/div')
driver.execute_script("arguments[0].scrollIntoView();", buy)
buy.click()

def buy_mobile():
try:
# 立即购买
buybtn = None
while None == buybtn:
buybtn = choose('/html/body/div[1]/div[2]/div/div[1]/div[2]/div')
driver.execute_script("arguments[0].scrollIntoView();", buybtn)
buybtn.click()
# 默认已经选好时间了,再点击立即购买
buy = None
while None == buy:
buy = choose('/html/body/div[1]/div[3]/div[2]/div[1]/div')
driver.execute_script("arguments[0].scrollIntoView();", buy)
buy.click()
# 580票面
price = None
while None == price:
price = choose('//html/body/div[1]/div/div[2]/ul/li[3]')
driver.execute_script("arguments[0].scrollIntoView();", price)
price.click()
# 数量+1
count = None
while None == count:
count = choose('/html/body/div[1]/div/div[3]/ul/li/div/div[3]')
driver.execute_script("arguments[0].scrollIntoView();", count)
count.click()
# 选好了
select = None
while None == select:
select = choose('/html/body/div[1]/div/div[4]/div[3]')
driver.execute_script("arguments[0].scrollIntoView();", select)
select.click()
# 购票人
booker = None
while None == booker:
booker = choose('/html/body/div[1]/div[1]/div[2]/div[1]/div[2]/div/ul/li/div')
driver.execute_script("arguments[0].scrollIntoView();", booker)
booker.click()
# 去付款
pay = None
while None == pay:
pay = choose('/html/body/div[1]/div[2]/div[2]/div[2]/div')
driver.execute_script("arguments[0].scrollIntoView();", pay)
pay.click()
except Exception:
print("抢票失败,尝试重新抢票")
buy_mobile()

def test_mobile():
login_mobile()
time.sleep(30)
print("开始抢票")
buy_mobile()
print("抢票成功")

def main():
# 默认PC网页,手机网页对应修改即可
login()
# 30秒等待用户输入密码后再开始刷
time.sleep(30)
while 1:
if MIN == time.localtime().tm_min:
print("开始抢票")
buy()
print("抢票成功")

if __name__ == '__main__':
# test()
test_mobile()
# main()