人狠话不多,直接上代码。 环境是python2.7.。需要安装requests模块。
# -*- coding: utf-8 -*-
import requests
import json
import smtplib #加载smtplib模块
from email.mime.text import MIMEText
from email.utils import formataddr
from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
def sendmail(msg):
receiver = ['*********@qq.com','*********@qq.com'] # shou件人邮箱账号,为了后面易于维护,所以写成了变量
sender = '*******@qq.com' # fa件人邮箱账号,需要开通发送邮件服务,为了后面易于维护,所以写成了变量 gmlfdflygtrnecce rrlwdujfvbzgecea
try:
content =msg
msg = MIMEText(content, 'html', 'utf-8')
msg['From'] = formataddr(["一个好人", sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号 一个好人
msg['To'] = formataddr(["sunny", receiver]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号 随心所欲
msg['Subject'] = "天气提示" # 邮件的主题,也可以说是标题
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是25
server.login(sender, "gmlfdflygtrnecce") # 括号中对应的是发件人邮箱账号、邮箱密码 530860979 jdfzpmrcowtnbhfh
server.sendmail(sender, receiver, msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 这句是关闭连接的意思
except Exception, e: # 如果try中的语句没有执行,则会执行下面的ret=False
print e
def tick():
print('Tick! The time is: %s' %datetime.now())
# city = raw_input("input city")
#用的是京东天气的接口,可以很容易的申请到。免费
params = {'city': 'shenzhen', 'appkey': '你的appkey'}
r = requests.get('https://way.jd.com/he/freeweather', params=params)
# print(r.status_code)
wjson = json.loads(r.content)
# data=json.dumps(wjson, ensure_ascii=False)
# data1 = json.loads(data)
'''
目前天气情况:实况天气
'''
# print wjson['result']['HeWeather5'][0]['now']
# msg= u'相对湿度:'+wjson['result']['HeWeather5'][0]['now']['hum']
# msg +=u'\n能见度:'+wjson['result']['HeWeather5'][0]['now']['vis']
# msg += u'\n大气压:'+ wjson['result']['HeWeather5'][0]['now']['pres']
# msg += u'\n降水量:'+ wjson['result']['HeWeather5'][0]['now']['pcpn']
# msg += u'\n感冒指数:'+ wjson['result']['HeWeather5'][0]['now']['fl']
# msg += u'\n温度:'+wjson['result']['HeWeather5'][0]['now']['tmp']
# msg +=u'\n天气状况描述:'+wjson['result']['HeWeather5'][0]['now']['cond']['txt']
# msg += u'\n紫外线指数:'+wjson['result']['HeWeather5'][0]['suggestion']['uv']['txt']
# msg +=u'\n洗车指数:'+wjson['result']['HeWeather5'][0]['suggestion']['cw']['txt']
# msg += u'\n旅游指数:'+wjson['result']['HeWeather5'][0]['suggestion']['trav']['txt']
# msg += u'\nair:'+wjson['result']['HeWeather5'][0]['suggestion']['air']['txt']
# msg += u'\n舒适度指数:'+wjson['result']['HeWeather5'][0]['suggestion']['comf']['txt']
# msg +=u'\n穿衣指数:'+wjson['result']['HeWeather5'][0]['suggestion']['drsg']['txt']
# msg += u'\n运动指数:'+wjson['result']['HeWeather5'][0]['suggestion']['sport']['txt']
# msg += u'\n感冒指数:'+wjson['result']['HeWeather5'][0]['suggestion']['flu']['txt']
# print msg
'''
未来几个小时的天气情况:
'''
# for i in range(len(wjson['result']['HeWeather5'][0]['hourly_forecast'])):
# #print wjson['result']['HeWeather5'][0]['hourly_forecast'][i]
# print '温度:',wjson['result']['HeWeather5'][0]['hourly_forecast'][i]['tmp']
# print '相对湿度:', wjson['result']['HeWeather5'][0]['hourly_forecast'][i]['hum']
# print '降水概率:', wjson['result']['HeWeather5'][0]['hourly_forecast'][i]['pop']
# print '天气状况描述:', wjson['result']['HeWeather5'][0]['hourly_forecast'][i]['cond']['txt']
# print '风力情况:', wjson['result']['HeWeather5'][0]['hourly_forecast'][i]['wind']['dir']
# print '风力等级:', wjson['result']['HeWeather5'][0]['hourly_forecast'][i]['wind']['sc']
# print '日期:', wjson['result']['HeWeather5'][0]['hourly_forecast'][i]['date']
# print wjson["msg"]
table_houres = u'''
<table width="800" border="1" cellspacing="0" cellpadding="2">
<tr>
<th>未来几小时天气</th>
<th >天气情况</th>
<th >温度</th>
<th >风力情况</th>
</tr>
''';
for i in range(len(wjson['result']['HeWeather5'][0]['hourly_forecast'])):
table_houres += u'<tr><td bgcolor="#EFEBDE" height="50" style="font-size:15px">' + \
wjson['result']['HeWeather5'][0]['hourly_forecast'][i][
'date'] + u'</td>' + u'<td bgcolor="#EFEBDE" height="50" style="font-size:15px">' + \
wjson['result']['HeWeather5'][0]['hourly_forecast'][i]['cond'][
'txt'] + u'</td>' + u'<td bgcolor="#EFEBDE" height="50" style="font-size:15px">' + \
wjson['result']['HeWeather5'][0]['hourly_forecast'][i][
'tmp'] + u'℃' + u'</td>' + u'<td bgcolor="#EFEBDE" height="50" style="font-size:15px">' + \
wjson['result']['HeWeather5'][0]['hourly_forecast'][i]['wind']['dir'] + u'</td></tr>'
table_houres += u'</table>'
'''
未来几天的天气情况:
'''
# for i in range(6):
# print '日期:',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['date']
# print '降水概率:',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['pop']
# print '相对湿度:',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['hum']
# print '紫外线指数:',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['uv']
# print '能见度:',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['vis']
# print '温度min:',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['tmp']['min']
# print '温度max:',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['tmp']['max']
# print '白天天气',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['cond']['txt_d']
# print '晚上天气',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['cond']['txt_n']
# print '风力等级:',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['wind']['sc']
# print '风力情况:',wjson['result']['HeWeather5'][0]['daily_forecast'][i]['wind']['dir']
table_days = u'''
<table width="800" border="1" cellspacing="0" cellpadding="2">
<tr>
<th>未来几天天气</th>
<th >白天天气</th>
<th >晚上天气</th>
<th>降水概率</th>
<th >风力情况</th>
<th >风力等级</th>
</tr>
''';
for i in range(len(wjson['result']['HeWeather5'][0]['daily_forecast'])):
table_days += u'<tr><td bgcolor="#EFEBDE" height="50" style="font-size:15px">' + \
wjson['result']['HeWeather5'][0]['daily_forecast'][i][
'date'] + u'</td>' + u'<td bgcolor="#EFEBDE" height="50" style="font-size:15px">' + \
wjson['result']['HeWeather5'][0]['daily_forecast'][i]['cond'][
'txt_d'] + u'</td>' + u'<td bgcolor="#EFEBDE" height="50" style="font-size:15px">' + \
wjson['result']['HeWeather5'][0]['daily_forecast'][i]['cond'][
'txt_n'] + u'℃' + u'</td>' + u'<td bgcolor="#EFEBDE" height="50" style="font-size:15px">' + \
wjson['result']['HeWeather5'][0]['daily_forecast'][i][
'pop'] + u'</td>' + u'<td bgcolor="#EFEBDE" height="50" style="font-size:15px">' + \
wjson['result']['HeWeather5'][0]['daily_forecast'][i]['wind'][
'dir'] + u'</td>' + u'<td bgcolor="#EFEBDE" height="50" style="font-size:15px">' + \
wjson['result']['HeWeather5'][0]['daily_forecast'][i]['wind']['sc'] + u'</td>' + u'</tr>'
table_days += u'</table></br></br>'
msg = u'''
<table width="800" border="1" cellspacing="0" cellpadding="2">
<tr>
<th>深圳今日天气</th>
<th width="80%">天气情况</th>
</tr>
<tr>
<td bgcolor="#EFEBDE" height="50" style="font-size:15px">
天气状况描述
</td>
<td bgcolor="#EFEBDE" colspan="2" height="50" style="font-size:15px">
''' + wjson['result']['HeWeather5'][0]['now']['cond']['txt'] + u'''
</td>
</tr>
<tr>
<td bgcolor="#EFEBDE" height="50" style="font-size:15px">
温度
</td>
<td bgcolor="#EFEBDE" colspan="2" height="50" style="font-size:15px">
''' + wjson['result']['HeWeather5'][0]['now']['tmp'] + u'℃' + u'''
</td>
</tr>
<tr>
<td bgcolor="#EFEBDE" height="50" style="font-size:15px">
紫外线指数
</td>
<td bgcolor="#EFEBDE" colspan="2" height="50" style="font-size:15px">
''' + wjson['result']['HeWeather5'][0]['suggestion']['uv']['txt'] + u'''
</td>
</tr>
<tr>
<td bgcolor="#EFEBDE" height="50" style="font-size:15px">
感冒指数
</td>
<td bgcolor="#EFEBDE" colspan="2" height="50" style="font-size:15px">
''' + wjson['result']['HeWeather5'][0]['suggestion']['flu']['txt'] + u'''
</td>
</tr>
<tr>
<td bgcolor="#EFEBDE" height="50" style="font-size:15px">
旅游指数
</td>
<td bgcolor="#EFEBDE" colspan="2" height="50" style="font-size:15px">
''' + wjson['result']['HeWeather5'][0]['suggestion']['trav']['txt'] + u'''
</td>
</tr>
<tr>
<td bgcolor="#EFEBDE" height="50" style="font-size:15px">
穿衣指数
</td>
<td bgcolor="#EFEBDE" colspan="2" height="50" style="font-size:15px">
''' + wjson['result']['HeWeather5'][0]['suggestion']['drsg']['txt'] + u'''
</td>
</tr>
<tr>
<td bgcolor="#EFEBDE" height="50" style="font-size:15px">
舒适度指数
</td>
<td bgcolor="#EFEBDE" colspan="2" height="50" style="font-size:15px">
''' + wjson['result']['HeWeather5'][0]['suggestion']['comf']['txt'] + u'''
</td>
</tr>
</table></br></br>''' + table_houres + table_days;
sendmail(msg)
if __name__ == '__main__':
scheduler = BlockingScheduler()
#定时功能,每天早上七点准时发邮件
scheduler.add_job(tick, 'cron', minute=0, hour='7', year='*', month='*')
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()