前言
最近武汉的天气越来越恶劣了。动不动就下雨,所以,拥有一款好的天气预报工具,对于我们大学生来说,还真是挺重要的了。好了,自己动手,丰衣足食,我们来用Python打造一个天气预报的微信机器人吧。
学习Python中有不明白推荐加入交流群
号:516107834
群里有志同道合的小伙伴,互帮互助,
群里有不错的学习教程!
效果展示
效果如下:
后台登录
收到天气预报消息:
环境配置
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()
网友评论