前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >10分钟教你用Python打造微信天气预报机器人

10分钟教你用Python打造微信天气预报机器人

作者头像
短短的路走走停停
发布2019-05-14 18:18:22
1.3K0
发布2019-05-14 18:18:22
举报
文章被收录于专栏:程序猿声程序猿声

前言

最近武汉的天气越来越恶劣了。动不动就下雨,所以,拥有一款好的天气预报工具,对于我们大学生来说,还真是挺重要的了。好了,自己动手,丰衣足食,我们来用Python打造一个天气预报的微信机器人吧。

效果展示

效果如下:

后台登录

收到天气预报消息:

环境配置

Python版本:3.6.0

系统平台:Windows 10 X64

相关模块:

json模块;

requests模块;

itchat模块;

以及一些Python自带的模块。

获取天气

主要原理很简单,找一个天气的API接口(这里我们使用的是http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=? ),使用requests发起请求,接受返回的结果,用python中内置的包json. 将json字符串转换为python的字典或列表,然后从字典中取出数据。

具体可以看代码:

 1    city = input('请输入要查询的城市名称:')
 2
 3    url = 'http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'%city
 4    # 使用requests发起请求,接受返回的结果
 5    rs = requests.get(url)
 6    # 使用loads函数,将json字符串转换为python的字典或列表
 7    rs_dict = json.loads(rs.text)
 8    # 取出error
 9    error_code = rs_dict['error']
10    # 如果取出的error为0,表示数据正常,否则没有查询到结果
11    if error_code == 0:
12        # 从字典中取出数据
13        results = rs_dict['results']
14        # 根据索引取出天气信息字典
15        info_dict = results[0]
16        # 根据字典的key,取出城市名称
17        city_name = info_dict['currentCity']
18        # 取出pm值
19        pm25 = info_dict['pm25']
20        print('当前城市:%s  pm值:%s'%(city_name,pm25))
21        # 取出天气信息列表
22        weather_data = info_dict['weather_data']
23        # for循环取出每一天天气的小字典
24        for weather_dict in weather_data:
25            # 取出日期,天气,风级,温度
26            date = weather_dict['date']
27            weather = weather_dict['weather']
28            wind = weather_dict['wind']
29            temperature = weather_dict['temperature']

注释很明了。相信大家都能get it!

发送天气预报

在获取到天气预报的数据以后,接下来就是通过itchat模块把信息发送到我们的微信上面了。原理也很简单,先扫码登录我们的微信机器人,然后通过备注名获取要发送的好友,send过去就OK啦。

具体看下面代码:

1# nickname = input('please input your firends\' nickname : ' )
2            #   想给谁发信息,先查找到这个朋友,name后填微信备注即可
3            # users = itchat.search_friends(name=nickname)
4            users = itchat.search_friends(name='起风了')  # 使用备注名来查找实际用户名
5            # 获取好友全部信息,返回一个列表,列表内是一个字典
6            print(users)
7            # 获取`UserName`,用于发送消息
8            userName = users[0]['UserName']
9            itchat.send(date+weather+wind+temperature, toUserName=userName)

说说怎么实现每天定时预报:

可以在程序加个while(True),然后每天定时获取天气,send过去。当然,你最好有一天云主机,把程序挂在主机上面就OK。

另一种实用的思路是:

收取消息关键字,然后回复天气。这个给大家思考实现啦。

完整代码

完整代码如下:

 1# 天气预告
 2# url 统一资源定位符
 3# windows + r  cmd 打开命令行工具 输入pip install requests
 4# 引入 requests
 5import requests
 6# 引入python中内置的包json. 用来解析和生成json数据的
 7import json
 8import itchat
 9
10
11def weather_main():
12    city = input('请输入要查询的城市名称:')
13
14    url = 'http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'%city
15    # 使用requests发起请求,接受返回的结果
16    rs = requests.get(url)
17    # 使用loads函数,将json字符串转换为python的字典或列表
18    rs_dict = json.loads(rs.text)
19    # 取出error
20    error_code = rs_dict['error']
21    # 如果取出的error为0,表示数据正常,否则没有查询到结果
22    if error_code == 0:
23        # 从字典中取出数据
24        results = rs_dict['results']
25        # 根据索引取出天气信息字典
26        info_dict = results[0]
27        # 根据字典的key,取出城市名称
28        city_name = info_dict['currentCity']
29        # 取出pm值
30        pm25 = info_dict['pm25']
31        print('当前城市:%s  pm值:%s'%(city_name,pm25))
32        # 取出天气信息列表
33        weather_data = info_dict['weather_data']
34        # for循环取出每一天天气的小字典
35        for weather_dict in weather_data:
36            # 取出日期,天气,风级,温度
37            date = weather_dict['date']
38            weather = weather_dict['weather']
39            wind = weather_dict['wind']
40            temperature = weather_dict['temperature']
41            print('%s %s %s %s'%(date,weather,wind,temperature))
42
43            # nickname = input('please input your firends\' nickname : ' )
44            #   想给谁发信息,先查找到这个朋友,name后填微信备注即可
45            # users = itchat.search_friends(name=nickname)
46            users = itchat.search_friends(name='起风了')  # 使用备注名来查找实际用户名
47            # 获取好友全部信息,返回一个列表,列表内是一个字典
48            print(users)
49            # 获取`UserName`,用于发送消息
50            userName = users[0]['UserName']
51            itchat.send(date+weather+wind+temperature, toUserName=userName)
52
53            print('succeed')
54
55    else:
56        print('没有查询到天气信息')
57
58
59itchat.auto_login()
60weather_main()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-11-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序猿声 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档