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


Posted in Python onMarch 25, 2019

0 引言

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

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

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

1 环境

操作系统:Windows / Linux

Python版本:3.7.2

2 代码实现

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

2.0 准备工作

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

pip install wxpy
pip install requests

 2.1 获取天气信息

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

http://t.weather.sojson.com/api/weather/city/city_code

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

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

{
 "_id": 58,
 "id": 59,
 "pid": 3,
 "city_code": "101230201",
 "city_name": "厦门"
}

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

# weather API的URL,此处的城市编号,参看_city.json
url = 'http://t.weather.sojson.com/api/weather/city/101010200'
header = {
 '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'
}

# 请求Weather API并拿到服务器返回的数据
rep = requests.get(url, headers = header)
rep.encoding = "utf-8"
result = ''
weather = rep.tex

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

"cityInfo": {
 "city": "海淀区", //请求城市
 "cityId": "101010200", //城市ID
 "parent": "北京市", //上级,一般是省份
 "updateTime": "09:02" //天气更新时间
}

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

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

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

"forecast": [ //今天+未来14天
 {
  "date": "16", //日期
  "sunrise": "06: 28",
  "high": "高温 20.0℃",
  "low": "低温 2.0℃",
  "sunset": "18: 21",
  "aqi": 48.0, 
  "ymd": "2019-03-16", //年月日
  "week": "星期六",
  "fx": "西北风", //风向
  "fl": "3-4级", //风力
  "type": "晴",
  "notice": "愿你拥有比阳光明媚的心情"
 }
]

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

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

# 解析服务器返回的数据,具体可参考weather.json文件
index_cityInfo = weather.find("cityInfo")
index_cityId = weather.find("cityId")
index_shidu = weather.find("shidu")
index_pm25 = weather.find("pm25")
index_pm10 = weather.find("pm10")
index_quality = weather.find("quality")
index_wendu = weather.find("wendu")
index_ganmao = weather.find("ganmao")
index_forecast = weather.find("forecast")
index_ymd = weather.find("ymd", index_forecast)
index_week = weather.find("week", index_forecast)
index_sunset = weather.find("sunset", index_forecast)
index_high = weather.find("high", index_forecast)
index_low = weather.find("low", index_forecast)
index_fx = weather.find("fx", index_forecast)
index_fl = weather.find("fl", index_forecast)
index_aqi = weather.find("aqi", index_forecast)
index_type = weather.find("type", index_forecast)
index_notice = weather.find("notice", index_forecast)

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

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

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

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

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

2.2 登录微信定时发送消息

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

# 初始化机器人,扫码登陆微信,适用于Windows系统
bot = Bot()

# Linux系统,执行登陆请调用下面的这句
bot = Bot(console_qr=2, cache_path="botoo.pkl")

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

# 调用get_weather函数
GW = get_weather()
# 填入你朋友的微信昵称,注意这里不是备注,也不是微信帐号
my_friend = bot.friends().search(u'一个昵称')[0]
# 发送微信消息
my_friend.send(u"早上好Y(^o^)Y,这里是今日份的天气信息请查收!")
my_friend.send(GW) 
my_friend.send(u"Have a Nice Day!")

# 每隔86400秒(1天),发送1次
t = Timer(86400, auto_send)
t.start()

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

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

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

# 调用函数进行消息发送
auto_send()

3 效果展示

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

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

4 后记

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

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

以上所述是小编给大家介绍的用python自制微信机器人,定时发送天气预报详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
python字符串排序方法
Aug 29 Python
Python3中使用urllib的方法详解(header,代理,超时,认证,异常处理)
Sep 21 Python
CentOS 6.5下安装Python 3.5.2(与Python2并存)
Jun 05 Python
Python使用cx_Oracle调用Oracle存储过程的方法示例
Oct 07 Python
Python3实现简单可学习的手写体识别(实例讲解)
Oct 21 Python
python一键去抖音视频水印工具
Sep 14 Python
tensorflow如何批量读取图片
Aug 29 Python
tensorflow实现在函数中用tf.Print输出中间值
Jan 21 Python
Pyecharts 动态地图 geo()和map()的安装与用法详解
Mar 25 Python
Python利用PyPDF2库获取PDF文件总页码实例
Apr 03 Python
python实现取余操作的简单实例
Aug 16 Python
如何用 Python 子进程关闭 Excel 自动化中的弹窗
May 07 Python
Python3.5实现的三级菜单功能示例
Mar 25 #Python
使用Django简单编写一个XSS平台的方法步骤
Mar 25 #Python
Python for循环与range函数的使用详解
Mar 23 #Python
详解Python读取yaml文件多层菜单
Mar 23 #Python
详解Python数据分析--Pandas知识点
Mar 23 #Python
详解Python基础random模块随机数的生成
Mar 23 #Python
Python基本数据结构与用法详解【列表、元组、集合、字典】
Mar 23 #Python
You might like
全国FM电台频率大全 - 23 四川省
2020/03/11 无线电
弄了个检测传输的参数是否为数字的Function
2006/12/06 PHP
一步一步学习PHP(3) php 函数
2010/02/15 PHP
thinkPHP的表达式查询用法详解
2016/09/14 PHP
js点击更换背景颜色或图片的实例代码
2013/06/25 Javascript
JS实现选择TextArea内文本的方法
2015/08/03 Javascript
15个值得开发人员关注的jQuery开发技巧和心得总结【经典收藏】
2016/05/25 Javascript
详解handlebars+require基本使用方法
2016/12/21 Javascript
Vue学习笔记进阶篇之多元素及多组件过渡
2017/07/19 Javascript
node.js的Express服务器基本使用教程
2019/01/09 Javascript
uin-app+mockjs实现本地数据模拟
2020/08/26 Javascript
Python脚本文件打包成可执行文件的方法
2015/06/02 Python
Python的Django框架中的表单处理示例
2015/07/17 Python
Python实现中文数字转换为阿拉伯数字的方法示例
2017/05/26 Python
Python实现简易Web爬虫详解
2018/01/03 Python
python使用tkinter实现简单计算器
2018/01/30 Python
python 剪切移动文件的实现代码
2018/08/02 Python
Python连接Redis的基本配置方法
2018/09/13 Python
Python3爬虫全国地址信息
2019/01/05 Python
python数据预处理之数据标准化的几种处理方式
2019/07/17 Python
python+selenium 点击单选框-radio的实现方法
2019/09/03 Python
解决django后台管理界面添加中文内容乱码问题
2019/11/15 Python
selenium+python配置chrome浏览器的选项的实现
2020/03/18 Python
python3环境搭建过程(利用Anaconda+pycharm)完整版
2020/08/19 Python
使用Python将xmind脑图转成excel用例的实现代码(一)
2020/10/12 Python
vivo智能手机官方商城:vivo
2016/09/22 全球购物
美国花布包包品牌:Vera Bradley
2017/08/11 全球购物
美国最大和最受信任的二手轮胎商店:Bestusedtires.com
2020/06/02 全球购物
北京银河万佳Java面试题
2012/03/21 面试题
中海讯通笔试题
2015/09/15 面试题
计算机专业毕业生求职信分享
2013/12/24 职场文书
工作会议欢迎词
2014/01/16 职场文书
秋季校运动会广播稿
2014/02/23 职场文书
大学生学习计划书
2014/09/15 职场文书
公安个人四风问题对照检查及整改措施
2014/10/28 职场文书
2015年大学迎新晚会总结
2015/07/16 职场文书