-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutomatic_ticket_purchase.py
executable file
·220 lines (187 loc) · 9.36 KB
/
Automatic_ticket_purchase.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
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
212
213
214
215
216
217
218
219
220
# -*- coding: utf-8 -*-
import os
import time
import pickle
from time import sleep
from selenium import webdriver
# 大麦网主页
damai_url = "https://www.damai.cn/"
# 登录页
login_url = "https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F"
# 抢票目标页
target_url = "https://detail.damai.cn/item.htm?spm=a2oeg.home.card_0.ditem_2.591b23e1M1AdAi&id=599834886497"
# target_url = "https://detail.damai.cn/item.htm?spm=a2oeg.search_category.0.0.1c114d15Ml5T1a&id=598315677461&clicktitle=2019%20%E5%BC%A0%E9%9F%B6%E6%B6%B5%E3%80%8C%E5%AF%93%E8%A8%80%E3%80%8D%E4%B8%96%E7%95%8C%E5%B7%A1%E5%9B%9E%E6%BC%94%E5%94%B1%E4%BC%9A-%E8%8B%8F%E5%B7%9E%E7%AB%99"
name = "大某人"
phone = "130xxxxx327"
class Concert(object):
def __init__(self):
self.status = 0 # 状态,表示如今进行到何种程度
self.login_method = 1 # {0:模拟登录,1:Cookie登录}自行选择登录方式
def set_cookie(self):
self.driver.get(damai_url)
print("###请点击登录###")
while self.driver.title.find('大麦网-全球演出赛事官方购票平台') != -1:
sleep(1)
print("###请扫码登录###")
while self.driver.title == '大麦登录':
sleep(1)
print("###扫码成功###")
pickle.dump(self.driver.get_cookies(), open("cookies.pkl", "wb"))
print("###Cookie保存成功###")
self.driver.get(target_url)
def get_cookie(self):
try:
cookies = pickle.load(open("cookies.pkl", "rb")) # 载入cookie
for cookie in cookies:
cookie_dict = {
'domain': '.damai.cn', # 必须有,不然就是假登录
'name': cookie.get('name'),
'value': cookie.get('value'),
"expires": "",
'path': '/',
'httpOnly': False,
'HostOnly': False,
'Secure': False}
self.driver.add_cookie(cookie_dict)
print('###载入Cookie###')
except Exception as e:
print(e)
def login(self):
if self.login_method == 0:
self.driver.get(login_url) # 载入登录界面
print('###开始登录###')
elif self.login_method == 1:
if not os.path.exists('cookies.pkl'): # 如果不存在cookie.pkl,就获取一下
self.set_cookie()
else:
self.driver.get(target_url)
self.get_cookie()
def enter_concert(self):
print('###打开浏览器,进入大麦网###')
self.driver = webdriver.Chrome() # 默认Chrome浏览器
self.driver.maximize_window() # 最大化窗口
self.login() # 先登录再说
# self.driver.refresh() #刷新页面
self.status = 2 # 登录成功标识
print("###登录成功###")
def choose_ticket(self):
if self.status == 2: # 登录成功入口
self.num = 1 # 第一次尝试
# print("="*30)
# print("###开始进行日期及票价选择###")
# 如果跳转到了订单结算界面就算这步成功了,否则继续执行此步
while self.driver.title.find('确认订单') == -1:
# time.sleep(0.5)
try:
self.driver.find_elements_by_xpath(
'//html//body//div[@class = "perform__order__box"]//div[6]//div[2]//div//div[2]')[0].click() # 选择票价
except:
print("购票档次选择失败")
time.sleep(0.5)
try:
self.driver.find_elements_by_xpath(
'//html//body//div[@class = "perform__order__price"]//div[2]//div//div//a[2]')[0].click() # 购票数+1(若需要)
except:
print("购票数添加失败")
# try:
# self.driver.find_elements_by_xpath('//div[@class = "perform__order__select perform__order__select__performs"]//div[2]//div//div[x]')[0].click() #默认购票日期的选择,x为日期的选择,1,2,3....
# except:
# print("日期选择失败")
# try:各种按钮的点击
buybutton = self.driver.find_element_by_class_name(
'buybtn').text
try:
buybutton = self.driver.find_element_by_class_name(
'buybtn').text
if buybutton == "即将开抢":
self.status = 2
self.driver.get(target_url)
print('###抢票未开始,刷新等待开始###')
continue
elif buybutton == "立即预定":
self.driver.find_element_by_class_name(
'buybtn').click()
self.status = 3
self.num = 1
elif buybutton == "立即购买":
self.driver.find_element_by_class_name(
'buybtn').click()
self.status = 4
elif buybutton == "立即预订":
self.driver.find_element_by_class_name(
'buybtn').click()
self.status = 3
self.num = 1
# 选座购买暂时无法完成自动化
elif buybutton == "选座购买":
self.driver.find_element_by_class_name(
'buybtn').click()
self.status = 5
elif buybutton == "提交缺货登记":
print('###抢票失败,请手动提交缺货登记###')
self.status = 2
self.driver.get(target_url)
continue
# break
except:
print('###未跳转到订单结算界面###')
title = self.driver.title
if title == "确认订单":
self.check_order()
# 若是选座购买,自行选座
elif self.status == 5:
print("###请自行选择位置和票价###")
break
if title != "确认订单": # 如果前一次失败了,那就刷新界面重新开始
self.status = 2
self.driver.get(target_url)
print('###抢票失败,从新开始抢票###')
def check_order(self):
if self.status in [3, 4]:
# print('###开始确认订单###')
# print('###默认购票人信息###')
# try:
# #姓名和电话的填写,这是绝对路径,由于大麦网目标页会更新,出现问题修改xpath即可
# time.sleep(0.5)
# self.driver.find_elements_by_xpath('//div[@class = "w1200"]//div[@class = "delivery-form"]//div[1]//div[2]//span//input')[0].send_keys(name)
# time.sleep(0.5)
# self.driver.find_elements_by_xpath('//div[@class = "w1200"]//div[@class = "delivery-form"]//div[2]//div[2]//span[2]//input')[0].send_keys(phone)
# time.sleep(0.5)
# except Exception as e:
# print("###填写确认订单信息时,联系人手机号填写失败###")
# print(e)
try:
time.sleep(0.5)
# 默认选第一个购票人信息
self.driver.find_elements_by_xpath(
'//div[@class = "w1200"]//div[@class = "ticket-buyer-select"]//div//div[2]//label//span')[0].click() # 观影人1
# 选第二个购票人信息(若购买多张票时需要开启此选项,增加购票人信息)
self.driver.find_elements_by_xpath(
'//div[@class = "w1200"]//div[@class = "ticket-buyer-select"]//div//div[3]//label//span')[0].click() # 观影人2
except Exception as e:
print("###购票人信息选中失败,自行查看元素位置###")
print(e)
# print('###不选择订单优惠###')
# print('###请在付款完成后下载大麦APP进入订单详情页申请开具###')
# 最后一步提交订单
time.sleep(0.5) # 太快会影响加载,导致按钮点击无效
self.driver.find_elements_by_xpath('//div[@class = "w1200"]//div[2]//div//div[9]//button[1]')[0].click()
time.sleep(10)
try:
element = WebDriverWait(self.driver, 1000).until(
EC.title_contains('支付宝 - 网上支付 安全快速!'))
self.status = 6
print('###成功提交订单,请手动支付###')
except:
print('###提交订单失败,请查看问题###')
# 若出现异常则关闭开启的浏览器及驱动,建议使用时注释掉,否则异常发生会关闭打开的浏览器
def finish(self):
self.driver.quit()
if __name__ == '__main__':
try:
con = Concert() # 具体如果填写请查看类中的初始化函数
con.enter_concert()
con.choose_ticket()
except Exception as e:
print(e)
# con.finish()