17
17
import codecs
18
18
from email .mime .text import MIMEText
19
19
20
+ import ssl
21
+ ssl ._create_default_https_context = ssl ._create_unverified_context
22
+
23
+ TRAIN = 'G7178'
24
+ # DELAY = 300
25
+ DATE = '2017-04-28'
26
+ FROM = 'SHH'
27
+ TO = 'NJH'
28
+
20
29
# 记录日志
21
30
def log (content ):
22
31
t = time .strftime ('%Y-%m-%d %H:%M:%S' )
@@ -26,10 +35,10 @@ def log(content):
26
35
27
36
# 发送邮件
28
37
def send_mail (content ):
29
- to_list = ['xxx@gmail .com' ] # 接收通知的邮箱
38
+ to_list = ['xxx@163 .com' ] # 接收通知的邮箱
30
39
mail_host = 'smtp.163.com' #, 587 #设置服务器
31
- mail_user = 'username ' #替换为发件邮箱用户名
32
- mail_pass = 'password ' #替换为发件邮箱口令
40
+ mail_user = '[email protected] ' #替换为发件邮箱用户名
41
+ mail_pass = 'zzz ' #替换为发件邮箱口令
33
42
mail_postfix = '163.com' #发件箱的后缀
34
43
me = "TicketsWatcher" + "<" + mail_user + "@" + mail_postfix + ">"
35
44
msg = MIMEText (content ,_subtype = 'plain' ,_charset = 'gb2312' )
@@ -45,8 +54,54 @@ def send_mail(content):
45
54
server .close ()
46
55
log ('sent mail successfully' )
47
56
57
+ def get_resp ():
58
+ # 设置网页请求地址
59
+ req_url = 'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT' % (
60
+ DATE , FROM , TO )
61
+ # 打开网页
62
+ resp = urllib2 .urlopen (req_url )
63
+ if resp :
64
+ print ('请求成功' )
65
+ # 读取网页内容
66
+ return resp .read ()
67
+
68
+ def get_data (resp_info ):
69
+ # 转换 json 数据
70
+ info_json = json .loads (resp_info )
71
+ info = info_json ['data' ]
72
+ # 遍历每车次信息
73
+ for train_data in info :
74
+ # 判断并获取需要查询的车次信息
75
+ train = train_data ['queryLeftNewDTO' ]
76
+ if train ['station_train_code' ] == TRAIN :
77
+ return train ['ze_num' ]
78
+ return '无'
79
+ # print '商务座:', train['swz_num']
80
+ # print '特等座', train['tz_num']
81
+ # print '一等座', train['zy_num']
82
+ # print '二等座', train['ze_num']
83
+ # print '高级软卧', train['gr_num']
84
+ # print '软卧', train['rw_num']
85
+ # print '硬卧', train['yw_num']
86
+ # print '软座', train['rz_num']
87
+ # print '硬座', train['yz_num']
88
+ # print '无座', train['wz_num']
89
+ # print '其它', train['qt_num']
48
90
49
91
try :
92
+ html_info = get_resp ()
93
+ tickets = get_data (html_info )
94
+ content = 'tickes for hard seat of %s: %s' % (TRAIN , tickets )
95
+ log (content )
96
+ if tickets != u'无' :
97
+ send_mail (content )
98
+ except Exception , e :
99
+ content = 'somethings wrong with the program:\n ' + str (e )
100
+ log (content )
101
+ send_mail (content ) # 出错时也发邮件
102
+ '''
103
+ # try:
104
+ if 1:
50
105
# 请求地址根据实际要抓取的页面修改,参数包括日期、出发站、到达站
51
106
resp = urllib2.urlopen("https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate=2016-06-03&from_station=NJH&to_station=SHH")
52
107
result = resp.read()
@@ -59,7 +114,9 @@ def send_mail(content):
59
114
if unicode(d['yz_num']) != u"无":
60
115
send_mail(content) # 如果不是“无”就发邮件通知
61
116
break
62
- except Exception , e :
117
+ # except Exception, e:
118
+ else:
63
119
content = 'somethings wrong with the program:\n ' + str(e)
64
120
log(content)
65
121
send_mail(content) # 出错时也发邮件
122
+ '''
0 commit comments