zoukankan      html  css  js  c++  java
  • 微信机器人11python自制微信机器人,定时发送天气预报

    python自制微信机器人,定时发送天气预报

    0 引言

    前段时间找到了一个免费的天气预报API,费了好段时间把这个API解析并组装成自己想用的格式了,就想着如何实现每天发送天气信息给自己。最近无意中发现了wxpy库,用它来做再合适不过了。以下是wxpy库的简介:

    wxpy基于itchat,使用了 Web 微信的通讯协议,通过大量接口优化提升了模块的易用性,并进行丰富的功能扩展。实现了微信登录、收发消息、搜索好友、数据统计、微信公众号、微信好友、微信群基本信息获取等功能。

    废话不多说,代码写起来。

    1 环境

    操作系统:Windows / Linux

    Python版本:3.7.2

    2 代码实现

    我们要实现用Python来发微信,发送的内容是每天最新的天气信息。很明显我们需要完成两部分的准备,先来看看获取天气信息这部分内容。

    2.0 准备工作

    本文我们用到的第三方库有requests、wxpyy,若环境还没有,按如下方式进行安装即可。

    1.  
      pip install wxpy
    2.  
      pip install requests
    3.  
      复制代码

    2.1 获取天气信息

    这里我使用的API的请求链接如下:

    t.weather.sojson.com/api/weather…

    请求方式是GET方法,使用时注意更换为自己城市对应的city_code,除此之外不用带任何参数。

    请求是restfull风格,city_code为9位数字,如下示例:

    1.  
      {
    2.  
      "_id": 58,
    3.  
      "id": 59,
    4.  
      "pid": 3,
    5.  
      "city_code": "101230201",
    6.  
      "city_name": "厦门"
    7.  
      }
    8.  
      复制代码

    大家可以从_city.json文件中获取各个城市对应的编号。该文件我已经放在Github本文章对应的目录下了,大家可自行查询使用。

    1.  
      # weather API的URL,此处的城市编号,参看_city.json
    2.  
      url = 'http://t.weather.sojson.com/api/weather/city/101010200'
    3.  
      header = {
    4.  
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'
    5.  
      }
    6.  
       
    7.  
      # 请求Weather API并拿到服务器返回的数据
    8.  
      rep = requests.get(url, headers = header)
    9.  
      rep.encoding = "utf-8"
    10.  
      result = ''
    11.  
      weather = rep.tex
    12.  
      复制代码

    这个API接口的返回值内容很多,以下仅展示返回的部分信息。实际使用中仅用到三块内容,首先是城市信息。

    1.  
      "cityInfo": {
    2.  
      "city": "海淀区", //请求城市
    3.  
      "cityId": "101010200", //城市ID
    4.  
      "parent": "北京市", //上级,一般是省份
    5.  
      "updateTime": "09:02" //天气更新时间
    6.  
      }
    7.  
      复制代码

    其次是,该城市当前天气的空气相关指数。

    1.  
      "data": {
    2.  
      "shidu": "32%", //湿度
    3.  
      "pm25": 35.0, //pm2.5
    4.  
      "pm10": 97.0, //pm10
    5.  
      "quality": "良", //空气质量
    6.  
      "wendu": "7", //温度
    7.  
      "ganmao": "极少数敏感人群应减少户外活动", //感冒提醒(指数)
    8.  
      }
    9.  
      复制代码

    第三部分,该城市当前天气的温度风力等另外一些指数。

    1.  
      "forecast": [ //今天+未来14天
    2.  
      {
    3.  
      "date": "16", //日期
    4.  
      "sunrise": "06: 28",
    5.  
      "high": "高温 20.0℃",
    6.  
      "low": "低温 2.0℃",
    7.  
      "sunset": "18: 21",
    8.  
      "aqi": 48.0,
    9.  
      "ymd": "2019-03-16", //年月日
    10.  
      "week": "星期六",
    11.  
      "fx": "西北风", //风向
    12.  
      "fl": "3-4级", //风力
    13.  
      "type": "晴",
    14.  
      "notice": "愿你拥有比阳光明媚的心情"
    15.  
      }
    16.  
      ]
    17.  
      复制代码

    注:这个API接口返回值完整的示例,请见Github中本文章目录下的weather.json文件。

    拿到返回值之后,需要解析,并转换组装成我们想要的格式。

    1.  
      # 解析服务器返回的数据,具体可参考weather.json文件
    2.  
      index_cityInfo = weather.find("cityInfo")
    3.  
      index_cityId = weather.find("cityId")
    4.  
      index_shidu = weather.find("shidu")
    5.  
      index_pm25 = weather.find("pm25")
    6.  
      index_pm10 = weather.find("pm10")
    7.  
      index_quality = weather.find("quality")
    8.  
      index_wendu = weather.find("wendu")
    9.  
      index_ganmao = weather.find("ganmao")
    10.  
      index_forecast = weather.find("forecast")
    11.  
      index_ymd = weather.find("ymd", index_forecast)
    12.  
      index_week = weather.find("week", index_forecast)
    13.  
      index_sunset = weather.find("sunset", index_forecast)
    14.  
      index_high = weather.find("high", index_forecast)
    15.  
      index_low = weather.find("low", index_forecast)
    16.  
      index_fx = weather.find("fx", index_forecast)
    17.  
      index_fl = weather.find("fl", index_forecast)
    18.  
      index_aqi = weather.find("aqi", index_forecast)
    19.  
      index_type = weather.find("type", index_forecast)
    20.  
      index_notice = weather.find("notice", index_forecast)
    21.  
      复制代码

    这是我最终想达到的效果如下:

    1.  
      # 今日天气预报
    2.  
      # 年月日 + 星期 + 所在地城市
    3.  
      # 天气类型 + 风向 + 风力
    4.  
      # 温度范围(最低温度~最高温度)
    5.  
      # 污染指数:PM2.5/PM10/AQI
    6.  
      # 空气质量
    7.  
      # 当前温度 + 空气湿度
    8.  
      # Notice信息
    9.  
      复制代码

    转换化具体代码就是这样子的:

    1.  
      result = '今日天气预报' + ' '
    2.  
      + weather[index_ymd + 6:index_week - 3] + " "
    3.  
      + weather[index_week + 7:index_fx - 3] + " "
    4.  
      + weather[index_cityInfo + 19:index_cityId - 3] + ' '
    5.  
      + "天气: " + weather[index_type + 7:index_notice - 3] + " "
    6.  
      + weather[index_fx + 5:index_fl - 3]
    7.  
      + weather[index_fl + 5:index_type - 3] + ' '
    8.  
      + "温度范围:" + weather[index_low + 9:index_sunset - 3] + " ~"
    9.  
      + weather[index_high + 10:index_low - 3] + ' '
    10.  
      + "污染指数: PM2.5:" + weather[index_pm25 + 6:index_pm10 - 1] + ""
    11.  
      + "PM10:" + weather[index_pm10 + 6:index_quality - 1] + " "
    12.  
      + "AQI:" + weather[index_aqi + 5:index_ymd - 2] + ' '
    13.  
      + "空气质量:" + weather[index_quality + 10:index_wendu - 3] + ' '
    14.  
      + "当前温度:" + weather[index_wendu + 8:index_ganmao - 3] + " "
    15.  
      + "空气湿度:" + weather[index_shidu + 8:index_pm25 - 3] + ' '
    16.  
      + weather[index_notice + 9:weather.find('}', index_notice) - 1]
    17.  
      复制代码

    这样我们的第一步,获取天气信息就完成了。接下来就是登录微信定时发送消息了。

    2.2 登录微信定时发送消息

    首先要登录微信,一行代码就搞定了。这里实际上是扫二维码登录了一个Web版的微信。

    1.  
      # 初始化机器人,扫码登陆微信,适用于Windows系统
    2.  
      bot = Bot()
    3.  
       
    4.  
      # Linux系统,执行登陆请调用下面的这句
    5.  
      bot = Bot(console_qr=2, cache_path="botoo.pkl")
    6.  
      复制代码

    然后我们需要定义一个发送消息的函数,将获取并解析好的天气信息发送给指定微信好友。

    1.  
      # 调用get_weather函数
    2.  
      GW = get_weather()
    3.  
      # 填入你朋友的微信昵称,注意这里不是备注,也不是微信帐号
    4.  
      my_friend = bot.friends().search(u'一个昵称')[0]
    5.  
      # 发送微信消息
    6.  
      my_friend.send(u"早上好Y(^o^)Y,这里是今日份的天气信息请查收!")
    7.  
      my_friend.send(GW)
    8.  
      my_friend.send(u"Have a Nice Day!")
    9.  
       
    10.  
      # 每隔86400秒(1天),发送1次
    11.  
      t = Timer(86400, auto_send)
    12.  
      t.start()
    13.  
      复制代码

    接下来,你可以使用try...except...语句来实现在消息失败时发出告警:

    1.  
      try:
    2.  
      '''此处为发送消息的代码,即上一段内容'''
    3.  
      except:
    4.  
      # 你的微信昵称,注意这里不是备注,也不是微信帐号
    5.  
      my_friend = bot.friends().search('&娴敲棋子&')[0]
    6.  
      my_friend.send(u"报告老板,今日份的信息发送失败了!")
    7.  
      复制代码

    最后运行主函数,调用发送消息的函数即可。

    1.  
      # 调用函数进行消息发送
    2.  
      auto_send()
    3.  
      复制代码

    3 效果展示

    这是我清晨收到的微信消息截图,看上去还不错。没白忙活?

    4 后记

    我把这个脚本丢在了我的树莓上,挂在后台一直运行,简直完美。

    这里仅是实现一个最简单的定时发送,后续考虑如何实现多个时间点的定时发送,还准备加上早间新闻资讯以及火车放票信息等内容。

    关注公众号「Python专栏」,后台回复:zsxq05,获取本文全套代码!

    转载于:https://juejin.im/post/5c9772f95188252d805c7a6b

  • 相关阅读:
    HDU6301 SET集合的应用 贪心
    线段树与树状数组的对比应用
    树状数组
    JDBC链接MySQL数据库
    HDU4686Arc of Dream 矩阵快速幂
    HDU1757矩阵快速幂
    B1013. 数素数 (20)
    B1023. 组个最小数 (20)
    [教材]B1020. 月饼 (25)
    [教材]A1025. PAT Ranking (25)
  • 原文地址:https://www.cnblogs.com/xinxihua/p/12820061.html
Copyright © 2011-2022 走看看