用Python定时发送天气邮件
作者:See?you?again31 时间:2022-09-22 15:11:31
效果如图
一、获取天气
def getWeather1(city):
try:
appid = os.environ["TIANQI_APPID"]
appsecret = os.environ["TIANQI_APPSEC"]
except KeyError:
appid = 'x'x'x'x' #www.tianqiapi.com申请的appid,有免费 api
appsecret = 'xxxx' #在www.tiaSnqiapi.com申请的appsecret
url = 'https://tianqiapi.com/api?version=v1&city={city}&appid={appid}&appsecret={appsecret}'.format(city=city,
appid=appid,
appsecret=appsecret)
res = requests.get(url)
if res.json().get("errcode", 0) > 0:
print(res.json().get("errmsg"))
exit(0)
data = res.json()['data']
weather = {
'today': data[0],
'tomorrow': data[1],
'aftertomorrow': data[2]
}
today = weather['today']
tomorrow = weather['tomorrow']
aftertomorrow = weather['aftertomorrow']
today_avg = (int(today['tem1'][:-1]) + int(today['tem2'][:-1])) / 2
tomorrow_avg = (int(tomorrow['tem1'][:-1]) + int(tomorrow['tem2'][:-1])) / 2
wdc ='紫外线指数:'+today['index'][0]['level'] +'\n'+ \
'穿衣指数:'+today['index'][3]['desc']+'\n'
wdc += 'tips:'+today['air_tips']
today_w = '今天 {} {}/{} 风力:{} 空气指数: {}/{} 日出日落: {}/{}'.format(today['wea'], today['tem1'], today['tem2'],today['win_speed'],today['air'],
today['air_level'], today['sunrise'], today['sunset'])
tomorrow_w = '明天 {} {}/{} 风力:{} 空气指数:{}/{} 日出日落: {}/{}'.format(tomorrow['wea'], tomorrow['tem1'], tomorrow['tem2'],tomorrow['win_speed'],tomorrow['air'],
tomorrow['air_level'], tomorrow['sunrise'],
tomorrow['sunset'])
aftertomorrow_w = '后天 {} {}/{} 风力:{} 空气指数:{}/{} 日出日落: {}/{}'.format(aftertomorrow['wea'], aftertomorrow['tem1'],
aftertomorrow['tem2'],aftertomorrow['win_speed'],aftertomorrow['air'],
aftertomorrow['air_level'], aftertomorrow['sunrise'],
aftertomorrow['sunset'])
todaytime = datetime.now()
starttime = datetime.strptime('2020-08-21','%Y-%m-%d')
days = (todaytime-starttime).days
todaydate = str(todaytime.year) + '年' + str(todaytime.month) + '月' + str(todaytime.day) + '日'
total = '早安! 亲爱的xx,xxxxx~愿你每天开开心心!\n'+ \
'今天是:'+todaydate+','+'是和xxx在一起的第'+str(days)+'天,mua~\n'+ \
'近日天气如下,xxx要注意保暖哦!\n'+ \
today_w + '\n' + wdc +'\n'+ \
tomorrow_w + '\n' + \
aftertomorrow_w
return total
二、获取金山词霸每日一句
def get_news():
# 获取金山词霸的每日一句的英文和翻译
url = "http://open.iciba.com/dsapi/"
r = requests.get(url)
content = r.json()['content']
note = r.json()['note']
news = content + '\n' + \
note
return str(news)
三、获取Sweet word
def getSweetWord():
url = 'https://chp.shadiao.app/api.php'
res = requests.get(url)
return res.text
四、发送邮件
def sendemail(toaddr='', message=''):
fromaddr = 'xxxxx@qq.com' # 你的邮箱
password = 'xxxxxfslfbfgg' # 你的密码,注意不是qq密码
smtp_server = 'smtp.qq.com' # smtp地址
msg = MIMEText(message, 'plain', 'utf-8')
msg['From'] = _format_addr('xxx <%s>' % fromaddr)
msg['To'] = _format_addr('xxx <%s>' % toaddr)
todaytime = datetime.now()
starttime = datetime.strptime('2020-08-21', '%Y-%m-%d')
days = (todaytime - starttime).days
emailtitle= '爱你的第'+str(days)+'天'
msg['Subject'] = Header(emailtitle, 'utf-8').encode()
server = smtplib.SMTP_SSL(smtp_server, 465)
server.set_debuglevel(1)
server.login(fromaddr, password)
server.sendmail(fromaddr, [toaddr], msg.as_string())
server.quit()
return
五、组织信息,并发送
def dailymorning():
message = getWeather1('xxx') + '\n' + \
get_news() + '\n' + \
getSweetWord() + '\n' + \
'来自最爱你xxx'
receivers = [['xxxx@qq.com'], ['xxxxxx@qq.com']]
for i in range(len(receivers)):
dailyemail.sendemail(toaddr=receivers[i], message=message)
print('send receiver[{}] success'.format(receivers[i]))
六、win10系统设置定时启动程序。
来源:https://blog.csdn.net/weixin_62266352/article/details/122777546
标签:python,邮件,天气
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中的单行、多行、中文注释方法
2022-03-21 21:56:38
![](https://img.aspxhome.com/file/2023/8/78638_0s.jpg)
教你如何通过日志文件恢复MySQL数据
2024-01-29 07:11:07
django框架实现一次性上传多个文件功能示例【批量上传】
2022-03-30 09:00:43
![](https://img.aspxhome.com/file/2023/7/66767_0s.png)
python使用pygame模块实现坦克大战游戏
2022-01-15 01:30:38
![](https://img.aspxhome.com/file/2023/8/99418_0s.png)
python实现三子棋游戏
2021-11-20 04:10:37
![](https://img.aspxhome.com/file/2023/0/133350_0s.png)
sqlserver 千万数量级分页存储过程代码
2024-01-14 07:50:13
Python可变集合和不可变集合的构造方法大全
2021-10-23 12:39:04
python高级特性简介
2023-11-21 10:27:08
jQuery实现弹出带遮罩层的居中浮动窗口效果
2024-04-19 10:17:20
![](https://img.aspxhome.com/file/2023/2/135862_0s.png)
python pandas cumsum求累计次数的用法
2021-08-07 01:51:19
![](https://img.aspxhome.com/file/2023/3/100283_0s.png)
Python中input()函数的用法实例小结
2021-09-04 18:42:59
![](https://img.aspxhome.com/file/2023/8/78598_0s.png)
随感:交互设计两三事
2009-05-18 13:54:00
![](https://img.aspxhome.com/file/UploadPic/20095/18/e59bbe-42s.jpg)
Ubuntu 下 vim 搭建python 环境 配置
2022-04-27 21:25:17
json 转 mot17数据格式的实现代码 (亲测有效)
2022-03-06 09:23:53
![](https://img.aspxhome.com/file/2023/8/120898_0s.png)
python处理文本文件实现生成指定格式文件的方法
2022-01-07 21:41:43
Python如何实现爬取B站视频
2021-10-09 07:46:04
![](https://img.aspxhome.com/file/2023/7/112997_0s.jpg)
使用Python标准库中的wave模块绘制乐谱的简单教程
2023-11-20 14:21:35
![](https://img.aspxhome.com/file/2023/5/63525_0s.jpg)
用Python生成N层的杨辉三角的实现方法
2022-12-20 16:27:09
![](https://img.aspxhome.com/file/2023/4/87604_0s.png)
CentOS环境下安装Redis3.0及phpredis扩展测试示例
2023-11-21 18:20:05
![](https://img.aspxhome.com/file/2023/1/110141_0s.png)
记Qzone项目组视觉设计标注的前前后后
2010-03-24 18:03:00
![](https://img.aspxhome.com/file/UploadPic/20103/24/1321290-25s.jpg)