Skip to content

Commit 56242c8

Browse files
committed
change email setting
1 parent 7dabbdf commit 56242c8

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

ticket/ticket_with_email.py

+61-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
import codecs
1818
from email.mime.text import MIMEText
1919

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+
2029
# 记录日志
2130
def log(content):
2231
t = time.strftime('%Y-%m-%d %H:%M:%S')
@@ -26,10 +35,10 @@ def log(content):
2635

2736
# 发送邮件
2837
def send_mail(content):
29-
to_list=['xxx@gmail.com'] # 接收通知的邮箱
38+
to_list=['xxx@163.com'] # 接收通知的邮箱
3039
mail_host = 'smtp.163.com' #, 587 #设置服务器
31-
mail_user = 'username' #替换为发件邮箱用户名
32-
mail_pass = 'password' #替换为发件邮箱口令
40+
mail_user = '[email protected]' #替换为发件邮箱用户名
41+
mail_pass = 'zzz' #替换为发件邮箱口令
3342
mail_postfix = '163.com' #发件箱的后缀
3443
me = "TicketsWatcher"+"<"+mail_user+"@"+mail_postfix+">"
3544
msg = MIMEText(content,_subtype='plain',_charset='gb2312')
@@ -45,8 +54,54 @@ def send_mail(content):
4554
server.close()
4655
log('sent mail successfully')
4756

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']
4890

4991
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:
50105
# 请求地址根据实际要抓取的页面修改,参数包括日期、出发站、到达站
51106
resp = urllib2.urlopen("https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate=2016-06-03&from_station=NJH&to_station=SHH")
52107
result = resp.read()
@@ -59,7 +114,9 @@ def send_mail(content):
59114
if unicode(d['yz_num']) != u"无":
60115
send_mail(content) # 如果不是“无”就发邮件通知
61116
break
62-
except Exception, e:
117+
# except Exception, e:
118+
else:
63119
content = 'somethings wrong with the program:\n' + str(e)
64120
log(content)
65121
send_mail(content) # 出错时也发邮件
122+
'''

0 commit comments

Comments
 (0)