Skip to content

Commit a28dcf6

Browse files
committed
兼容windows,防止读取city_code乱码;增加了一些注释
1 parent 6417f87 commit a28dcf6

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

‎hack12306.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import codecs
2525
import argparse
2626
import os
27+
import time
2728

2829
class hackTickets(object):
2930
"""docstring for hackTickets"""
3031

3132
"""读取配置文件"""
3233
def readConfig(self, config_file='config.ini'):
34+
print("加载配置文件...")
3335
# 补充文件路径,获得config.ini的绝对路径,默认为主程序当前目录
3436
path = os.path.join(os.getcwd(), config_file)
3537

@@ -90,8 +92,10 @@ def loadConfig(self):
9092
加载映射文件,并将中文"武汉"转换成编码后的格式:“武汉,WHN“
9193
"""
9294
def loadCityCode(self):
95+
print("映射出发地、目的地...")
9396
city_codes = {}
94-
with open('city_code.txt', 'r+') as f:
97+
path = os.path.join(os.getcwd(), 'city_code.txt')
98+
with codecs.open(path, "r", "utf-8-sig") as f:
9599
for l in f.readlines():
96100
city = l.split(':')[0]
97101
code = l.split(':')[1].strip()
@@ -113,6 +117,7 @@ def __init__(self):
113117
self.loadConfig();
114118

115119
def login(self):
120+
print("开始登录...")
116121
# 登录
117122
self.driver.visit(self.login_url)
118123
# 自动填充用户名
@@ -162,6 +167,7 @@ def preStart(self):
162167
self.driver.cookies.add({"_jc_save_fromDate": self.dtime})
163168

164169
def buyTickets(self):
170+
t = time.clock()
165171
try:
166172
print(u"购票页面开始...")
167173

@@ -231,6 +237,8 @@ def buyTickets(self):
231237
print(u"确认选座...")
232238
self.driver.find_by_id('qr_submit_id').click()
233239

240+
print (time.clock() - t)
241+
234242
except Exception as e:
235243
print(e)
236244

@@ -251,5 +259,8 @@ def start(self):
251259
self.buyTickets();
252260

253261
if __name__ == '__main__':
262+
print("===========hack12306 begin===========")
263+
begin = time.clock()
254264
hackTickets = hackTickets()
255265
hackTickets.start()
266+
print (time.clock() - begin)

0 commit comments

Comments
 (0)