-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp_autobuy_ticket.py
117 lines (112 loc) · 4.39 KB
/
mp_autobuy_ticket.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
from splinter.browser import Browser
from time import sleep
from datetime import datetime
from multiprocessing.dummy import Pool as ThreadPool
class Buy_Tickets(object):
def __init__(self,orders, passengers, dtime, starts, ends):
# 车次,0代表所有车次,依次从上到下,1代表列表显示第一班车次,依次类推
self.orders = orders
# 乘客名
self.passengers = passengers
# 起始地和终点
self.starts = starts
self.ends = ends
# 日期
self.dtime = dtime
# self.xb = xb
# self.pz = pz
self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html'
self.initMy_url = 'https://kyfw.12306.cn/otn/view/index.html'
self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init'
self.driver_name = 'chrome'
self.executable_path = 'chromedriver.exe'
#查询功能
def check(self,order):
try:
self.driver.find_by_text('预订')[order - 1].click()
sleep(1.5)
except Exception as e:
print(e)
print('预订失败...')
# 买票功能
def start_buy(self):
self.pool = ThreadPool()
self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path)
self.driver.driver.maximize_window()
#登录
self.driver.visit(self.login_url)
sleep(1)
while True:
if self.driver.url != self.initMy_url:
sleep(1)
else:
break
self.driver.visit(self.ticket_url)
try:
print('开始购票...')
# 加载查询信息
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.orders != 0:
while self.driver.url == self.ticket_url:
self.driver.find_by_text('查询').click()
count += 1
print('第%d次点击查询...' % count)
start = datetime.now()
self.pool.map(self.check,self.orders)
end = datetime.now()
print((end-start).total_seconds())
else:
while self.driver.url == self.ticket_url:
self.driver.find_by_text('查询').click()
count += 1
print('第%d次点击查询...' % count)
try:
for i in self.driver.find_by_text('预订'):
i.click()
sleep(1)
except Exception as e:
print(e)
print('预订失败...')
continue
print('开始预订...')
sleep(1)
print('开始选择用户...')
for p in self.passengers:
self.driver.find_by_text(p).last.click()
sleep(0.5)
if p[-1] == ')':
self.driver.find_by_id('dialog_xsertcj_ok').click()
print('提交订单...')
# sleep(1)
# self.driver.find_by_text(self.pz).click()
# sleep(1)
# self.driver.find_by_text(self.xb).click()
# sleep(1)
self.driver.find_by_id('submitOrder_id').click()
sleep(2)
print('确认选座...')
self.driver.find_by_id('qr_submit_id').click()
print('预订成功...')
self.pool.close()
self.pool.join()
except Exception as e:
print(e)
if __name__ == '__main__':
# 车次选择,0代表所有车次
orders = [2,3,4]
# 乘客名,比如passengers = ['XXX', 'XXX']
# 学生票需注明,注明方式为:passengers = ['XXX(学生)', 'XXX']
passengers = ['']
# 日期,格式为:'2018-01-20'
dtime = '2021-09-29'
# 出发地(需填写cookie值)
starts = ''
# 目的地(需填写cookie值)
ends = ''
# xb =['硬座座']
# pz=['成人票']
Buy_Tickets(orders , passengers, dtime, starts, ends).start_buy()