千家信息网

怎么用Python定时发送天气邮件

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章主要介绍"怎么用Python定时发送天气邮件"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"怎么用Python定时发送天气邮件"文章能帮助大家解决问题。
千家信息网最后更新 2026年11月04日怎么用Python定时发送天气邮件

这篇文章主要介绍"怎么用Python定时发送天气邮件"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"怎么用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系统设置定时启动程序。

关于"怎么用Python定时发送天气邮件"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。

天气 指数 邮件 知识 空气 风力 密码 行业 词霸 金山 不同 亲爱 实用 保暖 信息 内容 地址 实用性 实际 效果 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广东万易互联网科技有限公司惠州分公司 互联网科技的发展 互联网科技公司发展 善班互联网科技 世界互联网领先科技成果 华中科技大学互联网 上海亿帐通互联网科技 互联网科技公司名称 互联网科技项目计划书材料类 互联网和黑科技的看法 宜昌元轩互联网科技有限公司 河南住房之家互联网科技 城市宝互联网科技有限公司 互联网相关科技技术 互联网风格科技公司名称 九思连强互联网电子科技有限公司 萌小萌互联网科技河南有限公司 互联网时代高科技布局 互联网科技对化妆品行业 伍佰万互联网科技是干嘛的 喔噻互联网科技有限公司 华服互联网科技怎么样 互联网科技公司官网 科技创新互联网企业 易通互联网络科技有限公司 央行互联网金融科技公司 移动互联网科技股 河南住房之家互联网科技 高科技互联网发展前景如何 颐高云石互联网科技
0