用Python定时发送天气邮件
作者:See?you?again31 发布时间:2022-09-22 15:11:31
标签:python,邮件,天气
效果如图
一、获取天气
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过滤txt文件内重复内容,并将过滤后的内容保存到新的txt中示例如下 原文件处理之后的文件 直接上代码# -*-coding:u
- 创建 NumPy ndarray 对象NumPy 用于处理数组,NumPy 中的数组对象称为 ndarray。我们可以使用 array()
- PyAutoGUI是一个Python语言的键鼠自动化库,简单来说和按键精灵的功能一样。但是因为是Python的类库,所以可以使用Python
- 我们用python 打包的exe文件的时候,每次运行后面都有一个黑框框,比如我的这个:用tkinter做的图形界面,打包成exe文件,每次运
- 本篇文章将介绍:xlwt 常用功能xlrd 常用功能xlutils 常用功能xlwt写Excel时公式的应用xlwt写入特定目录(路径设置)
- 本文实例讲述了Python快速排序算法。分享给大家供大家参考,具体如下:快速排序的时间复杂度是O(NlogN)算法描述:① 先从序列中取出一
- 自己有一套模块化的思路,想搜索一下有没有共鸣结果排名靠前的是通过class拼凑页面的想法。模块化是twinsen提出来的,从我接收第一个po
- 这是个老话题了,之所以再拿出来说,是因为浏览器一直在进步,以前最好的方法现在不一定是最好的。1 如何进行字符串连接?首先让我们来回顾一下字符
- 目录总体思路:判断链接是否指向文件:下载文件:获取 url 下的所有链接:最近维基 jie mi 彻底公开了网站的全部文件,我就在想如何使用
- 众所周知,Jupyter notebook是一个交互式的Python shell,也就是IPython的封装版,非常适合用来进行数据分析和机
- 一、下载instant client1.附链接:http://www.oracle.com/technetwork/topics/winx6
- 一、异常检测简介异常检测是通过数据挖掘方法发现与数据集分布不一致的异常数据,也被称为离群点、异常值检测等等。1.1 异常检测适用的场景异常检
- --新增表字段 ALTER procedure [dbo].[sp_Web_TableFiled_Insert] ( @TableName
- 一个XML文档如果符合一些基本的规范,那它就是结构规范的。XML格式有一套比HTML简单的解析规则,允许XML解析器不需要外部描述或了解数据
- Socket有一个缓冲区,缓冲区是一个流,先进先出,发送和取出的可自定义大小的,如果取出的数据未取完缓冲区,则可能存在数据怠慢。其中【rec
- 1. 日志输出到屏幕#!/usr/bin/env python# -*- coding: utf-8 -*-from __future__
- 报“服务没有及时响应或控制请求”的错误,改用pyinstaller生成也是不行;查资料后修改setup.py如下即可,服务名、脚本名请自行替
- 1、空(None)表示该值是一个空对象,空值是Python里一个特殊的值,用None表示。None不能理解为0,因为0是有意义的,而None
- 什么是MySQL多实例简单地说,Mysql多实例就是在一台服务器上同时开启多个不同的服务端口(3306、3307),同时运行多个Mysql服
- 一、什么是组件组件 (Component) 是 Vue.js 最强大的功能之一。组件可以扩展 HTML 元素,封装可重用的代码。二、组件用法