美文网首页大数据 爬虫Python AI SqlPython编程学习交流中心
过年想有人跟着回家?Python智能化“天气提醒”帮你搞定小姐姐

过年想有人跟着回家?Python智能化“天气提醒”帮你搞定小姐姐

作者: 璃沫仙人 | 来源:发表于2019-01-03 10:57 被阅读0次

    前辈前几天发的文章,操作python那可是一流的666,分享过小编的可爱的小伙伴们,对没有学python和在学python的小伙伴来说,那绝对是精品中的精品。优秀上的优秀。最重要是教单身程序员怎么撩小姐姐哦!

    程序员和未来的程序员必学,跟着小编这个有了21年经验的小姐姐,一起学习下面的内容吧!

    开讲啦 开讲啦:

    需要的加Q君羊:八八三四四四一零六 。
    实现第一步,用“和风天气”API获取天气数据(当然你也可以选用其他的接口)

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">城市接口: https://api.heweather.com/x3/weather?cityid=城市ID&key=你的认证key
    </pre>

    update 和风天气的API11月初改版了,所以上述接口地址需要更换成新的,看了下就加了个“free-”,后面代码加上就好了。

    接口地址变了

    首先得注册登录和风天气获得key,在城市代码里面找到想要查的城市ID,以北京为例:CN101010100

    城市ID

    接着用API调试工具试试这个接口,看看返回的是什么数据。

    我用的是chrome下postman,打开postman,输入接口链接:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">https://api.heweather.com/x3/weather?cityid=CN101010100&key=这里填你的key
    </pre>

    API调试

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"> 返回的是JSON格式的,现在用Python调用一下这个接口试试:# -- coding: utf-8 --
    import urllib2

    调用和风天气的API

    url = 'https://api.heweather.com/x3/weather?cityid=CN101010100&key=你的key'

    用urllib2创建一个请求并得到返回结果

    req = urllib2.Request(url)
    resp = urllib2.urlopen(req).read()
    print resp
    </pre>

    python返回的数据:

    我们用postman看看结构就好了。

    Python内置数据类型和JSON的对应关系

    对应关系

    Python内置的json模块提供了非常完善的Python对象到JSON格式的转换。我们用loads()方法把JSON反序列化为Python对象

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">#将JSON转化为Python的数据结构
    json_data = json.loads(resp)
    print json_data
    </pre>

    结果发现数据变成了u'XXXXX'的格式,这是因为反序列化得到的所有字符串对象默认都是unicode编码的。

    结果:

    我们此时已经将天气数据拿到,并且反序列化为Python对象了,只有提取我们需要的数据即可。看下天气API提供哪些数据

    postman上的JSON结构

    我就每天早上听一下今天的气温、风力、pm2.5、穿衣建议就好了。

    现在以获得pm2.5的值为例,一层层获取到pm2.5的值

    pm2.5的值

    上面说了JSON里面{}对应的是Python的dict,[]对应的是Python里的list,上图中可以比较清楚地知道这个JSON得到的Python数据其实是dict和list的多层嵌套。这里我们可以类比查《新华字典》,想要查到“天气”这个词,可以找到字母T的页码(按A~Z的有序列表),再找到“tian”所在的位置,一步步往里找就成找到想要的词了。

    最外层是个dict类型:

    dict: {'HeWeather...": [ list]}

    往里一层是多个dict,直接通过多层dict['key']['key2']...就能取得PM2.5的值了:

    list 只有一个值,取list[0] (它的值是一个dict),取得天气只需要

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">print json_data['HeWeather data service 3.0'][0]['aqi']['city']['pm25']
    </pre>

    获取天气数据的代码:

    为什么没有放出全部的代码,因为小编还没有男朋友,小编群里的管理员小姐姐也没有哦!


    代码运行后:

    运行结果

    第二步、将文字合成语音:

    我直接用的百度的语音合成API,调用方法很简单(浏览器就可以玩)

    1、同样是注册登录获得key,百度语音

    2、注册个应用,随便啥应用都行,获得APPID

    这几步都可以参考官方接入指南

    3、获得token(一个月有效期,过期再获取即可)

    浏览器输入:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的AppID&client_secret=你的AppKey
    </pre>

    返回的JSON里面的“access_token”就是调用百度语音合成API要用的token

    4、合成语音 ,必填、选填参数可看官方文档

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">http://tsn.baidu.com/text2audio?tex=需要合成语音的文本&lan=zh&per=0&cuid=设备mac地址或手机的IMEI码&ctp=1&tok=你的token
    </pre>

    5、我们只需要把天气文本放在第4步的链接tex=' '里面就好了

    这么优秀的代码,不上手一下吗?

    第三步、用树莓派播放天气预报:

    树莓派上安装linux音频播放程序 mpg123,执行

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">sudo apt-get install mpg123
    </pre>

    这玩意使用起来异常简单,试试用命令行 “mpg123 语音url” 来播放在线的音频

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">mpg123 http://tsn.baidu.com/text2audio?tex=需要合成语音的文本&lan=zh&per=0&cuid=设备mac地址或手机的IMEI码&ctp=1&tok=你的token
    </pre>

    现在只要把第一步获得的天气文本用mpg123播放就好了,在第一步的代码基础上加上这些:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">url = "http://tsn.baidu.com/text2audio?tex=" + weather_forcast_txt + "&lan=zh&per=0&cuid=设备mac地址或手机的IMEI码&ctp=1&tok=你的token"
    os.system('mpg123 "%s"'%(url)) #需要import os
    </pre>

    最后,定时执行天气播报脚步就好了

    用linux的crontab,可查看crontab定时任务

    在终端执行

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">sudo crontab -e
    </pre>

    新增一条

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">0 8 * * * python /home/pi/...(你的天气播报脚本的路径)
    </pre>

    即每天8点整执行语音播报

    切记:得到代码后要跟着敲打一边,进行实操练习!

    是不是很赞哇~

    还不赶快动手操作下,做个定制版的“播报提醒”程序,暖心你的小姐姐,加油!

    愿有情人终成眷属。

    小编坐等学python的小姐姐来撩哦!

    相关文章

      网友评论

        本文标题:过年想有人跟着回家?Python智能化“天气提醒”帮你搞定小姐姐

        本文链接:https://www.haomeiwen.com/subject/ceffrqtx.html