北京的天气变化比较大,而且经常有恶劣天气,因此需要经常查看天气预报,不过每次手动百度太麻烦了,不妨采用点自动化的方法,首先调用一个天气网站的API,然后使用Python的一个第三方模块itchat来自动发送天气信息到微信上.
思路
- 调用和风天气的 API,获取天气数据
- 将天气数据使用itchat发送给自己
实现
第一步、用和风天气 API 获取天气数据
城市接口: https://api.heweather.com/x3/weather?cityid = 城市 ID&key = 你的认证 key
首先得注册登录和风天气 http://www.heweather.com/documents/api 获得 key, 在城市代码里面找到想要查的城市 ID,以北京为例:CN101010100
则 url = ‘https://api.heweather.com/x3/weather?cityid=CN101010100&key=12d3f43d4b9c48eb9d1d7e8902795aa2‘
然后就可以获取天气数据
第二步、调用itchat发送天气信息
首先安装itchat
pip install itchat
itchat示例如下
import itchat
# 登录
itchat.login()
# 发送消息
itchat.send(u'你好', 'filehelper')
获取第一步的天气数据后使用itchat.send给filehelper即可,手机微信上就会收到数据
Python完整实现代码如下
# coding:utf-8
# !/usr/bin/env python
# PyCharm Python2.7
# Created on 17-5-7
__Author__ = 'minning'
import json
import sys
import time
import urllib2
import itchat
reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入
sys.setdefaultencoding('utf-8') # 这个是解决合成中文文本的时候,Unicode 和 utf-8 编码问题的,可以尝试注释掉会不会报错
newInstance = itchat.new_instance()
newInstance.auto_login(hotReload=True, statusStorageDir='newInstance.pkl')
# itchat.auto_login(hotReload=True)
newInstance.login()
# newInstance.run()
while True:
# 调用和风天气的 API
url = 'https://api.heweather.com/x3/weather?cityid=CN101010100&key=12d3f43d4b9c48eb9d1d7e8902795aa2'
req = urllib2.Request(url)
resp = urllib2.urlopen(req).read()
# itchat.send(u'你好 minning', 'filehelper')
# 将 JSON 转化为 Python 的数据结构
json_data = json.loads(resp)
data = json_data['HeWeather data service 3.0'][0]
# 获取 PM2.5 的值
pm25 = data['aqi']['city']['pm25']
# 获取空气质量
air_quality = data['aqi']['city']['qlty']
# 获取城市
city = data['basic']['city']
# 获取现在的天气、温度、体感温度、风向、风力等级
now_weather = data['now']['cond']['txt']
now_tmp = data['now']['tmp']
now_fl = data['now']['fl']
now_wind_dir = data['now']['wind']['dir']
now_wind_sc = data['now']['wind']['sc']
# 今天的天气
today = data['daily_forecast'][0]
weather_day = today['cond']['txt_d']
weather_night = today['cond']['txt_n']
tmp_high = today['tmp']['max']
tmp_low = today['tmp']['min']
wind_dir = today['wind']['dir']
wind_sc = today['wind']['sc']
# 天气建议
# 舒适度
comf = data['suggestion']['comf']['brf']
comf_txt = data['suggestion']['comf']['txt']
# 流感指数
flu = data['suggestion']['flu']['brf']
flu_txt = data['suggestion']['flu']['txt']
# 穿衣指数
drsg = data['suggestion']['drsg']['brf']
drsg_txt = data['suggestion']['drsg']['txt']
newInstance.send("开始天气预报", 'filehelper')
time.sleep(1)
newInstance.send("{}".format(time.strftime('%Y年%m月%d日 %H:%M',time.localtime(time.time()))), 'filehelper')
time.sleep(1)
newInstance.send("地址 : {}".format('北京'), 'filehelper')
newInstance.send("pm25 : {}".format(pm25), 'filehelper')
newInstance.send("空气质量 : {}".format(air_quality), 'filehelper')
newInstance.send("天气 : {}".format(now_weather), 'filehelper')
newInstance.send("温度 : {}摄氏度".format(now_tmp), 'filehelper')
newInstance.send("气温 : {}".format(drsg), 'filehelper')
time.sleep(1)
newInstance.send("本次天气预报报道完毕", 'filehelper')
time.sleep(3600)