脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 用Python定时发送天气邮件

用Python定时发送天气邮件

2022-09-12 11:41See you again31 Python

大家好,本篇文章主要讲的是用Python定时发送天气邮件,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下

效果如图 

用Python定时发送天气邮件

一、获取天气

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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

二、获取金山词霸每日一句

?
1
2
3
4
5
6
7
8
9
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

?
1
2
3
4
def getSweetWord():
    url = 'https://chp.shadiao.app/api.php'
    res = requests.get(url)
    return res.text

四、发送邮件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

五、组织信息,并发送

?
1
2
3
4
5
6
7
8
9
10
11
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定时发送天气邮件的文章就介绍到这了,更多相关Python发送天气邮件内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_62266352/article/details/122777546

延伸 · 阅读

精彩推荐
  • PythonPython编程scoketServer实现多线程同步实例代码

    Python编程scoketServer实现多线程同步实例代码

    这篇文章主要介绍了Python编程scoketServer实现多线程同步实例代码,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下...

    张开开10432021-01-10
  • Pythonpython2.7到3.x迁移指南

    python2.7到3.x迁移指南

    由于PYTHON2.7即将停止支持,小编给大家分享了一篇关python2.7到3.x迁移指南内容,希望对各位有用。...

    脚本之家9102021-01-11
  • PythonPython OpenCV实现基本图形绘制

    Python OpenCV实现基本图形绘制

    这篇文章主要介绍了Python OpenCV实现基本图形绘制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面...

    牛牛牛转乾坤12092021-10-19
  • PythonScrapy实现模拟登录的示例代码

    Scrapy实现模拟登录的示例代码

    这篇文章主要介绍了Scrapy实现模拟登录的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    pengjunlee10332021-09-07
  • Python1秒钟使用python建立文件服务器的方法步骤

    1秒钟使用python建立文件服务器的方法步骤

    本文主要介绍了1秒钟使用python建立文件服务器的方法步骤,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    物联网老王11082022-02-10
  • PythonPython赋值语句后逗号的作用分析

    Python赋值语句后逗号的作用分析

    这篇文章主要介绍了Python赋值语句后逗号的作用,实例分析了Python赋值语句加入逗号实现类型转换的技巧,需要的朋友可以参考下 ...

    liuzx323602020-07-14
  • Pythonpython difflib模块示例讲解

    python difflib模块示例讲解

    这篇文章主要为大家详细介绍了python difflib模块的示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Lockeyi2832020-12-08
  • Pythonpython绘制云雨图raincloud plot

    python绘制云雨图raincloud plot

    这篇文章主要介绍了python绘制云雨图raincloud plot,Raincloud的Python实现是一个名为PtitPrince的包,它写在seaborn之上,这是一个Python绘图库,用于从pandas数据帧中...

    LIAN_U7332022-08-04