1. 相关链接
1. 和风天气 API 开发文档 : https://dev.qweather.com/docs/api/
2. 和风天气开发平台 : https://dev.qweather.com/
3. 地址查询 : http://www.weather.com.cn/
2. 前期准备
1. 注册和风天气开发平台
2. 和风天气控制台 - 应用管理 - 创建应用 - 免费开发版 .....
3. 获得 KEY
和风天气控制台 - 应用管理 - 复制创建的免费开发版的 KEY
(如 : fed098f400e84c8a9f029bcb72c1197d)
3. 获得 请求
进入开发者文档(如 : 逐天天气预报) - 请求URL - 3天预报 - 开发版 复制下边的链接
(如 : https://devapi.qweather.com/v7/weather/3d?[请求参数])
4. 获得 地址
进入 http://www.weather.com.cn/ 查询所需地址(如 : 平顶山) - 点击进入 - 复制网址中的地址数字
(如 : http://www.weather.com.cn/weather1d/101180501.shtml#input , 得到 : 101180501)
5. 结合
将上述的信息结合起来获得网址 : https://devapi.qweather.com/v7/weather/3d?location=地址&key=KEY
(如 : https://devapi.qweather.com/v7/weather/3d?location=101180501&key=fed098f400e84c8a9f029bcb72c1197d)
6. 将此网址输入浏览器可获得 :
{"code":"200","updateTime":"2021-05-03T11:35+08:00","fxLink":"http://hfx.link/2ra1","daily":[{"fxDate":"2021-05-03","sunrise":"05:37","sunset":"19:11","moonrise":"01:00","moonset":"11:05","moonPhase":"亏凸月","tempMax":"20","tempMin":"14","iconDay":"305","textDay":"小雨","iconNight":"101","textNight":"多云","wind360Day":"135","windDirDay":"东南风","windScaleDay":"1-2","windSpeedDay":"3","wind360Night":"315","windDirNight":"西北风","windScaleNight":"4-5","windSpeedNight":"34","humidity":"20","precip":"2.5","pressure":"996","vis":"24","cloud":"64","uvIndex":"3"},{"fxDate":"2021-05-04","sunrise":"05:36","sunset":"19:12","moonrise":"01:46","moonset":"12:09","moonPhase":"下弦月","tempMax":"26","tempMin":"11","iconDay":"104","textDay":"阴","iconNight":"101","textNight":"多云","wind360Day":"315","windDirDay":"西北风","windScaleDay":"4-5","windSpeedDay":"34","wind360Night":"315","windDirNight":"西北风","windScaleNight":"3-4","windSpeedNight":"16","humidity":"27","precip":"0.0","pressure":"997","vis":"25","cloud":"0","uvIndex":"11"},{"fxDate":"2021-05-05","sunrise":"05:35","sunset":"19:13","moonrise":"02:24","moonset":"13:12","moonPhase":"残月","tempMax":"25","tempMin":"15","iconDay":"104","textDay":"阴","iconNight":"150","textNight":"晴","wind360Day":"180","windDirDay":"南风","windScaleDay":"3-4","windSpeedDay":"16","wind360Night":"225","windDirNight":"西南风","windScaleNight":"3-4","windSpeedNight":"16","humidity":"36","precip":"0.0","pressure":"998","vis":"25","cloud":"0","uvIndex":"4"}],"refer":{"sources":["Weather China"],"license":["no commercial use"]}}
7. 排布
为了更加精确地获得数据,需要将上述数据排布处理(加一些回车便于观察)一下 :
{"code":"200","updateTime":"2021-05-02T15:35+08:00","fxLink":"http://hfx.link/2ra1",
"daily":
[
{"fxDate":"2021-05-02","sunrise":"05:38","sunset":"19:10","moonrise":"00:05","moonset":"10:00","moonPhase":"亏凸月","tempMax":"22","tempMin":"15","iconDay":"104","textDay":"阴","iconNight":"305","textNight":"小雨","wind360Day":"90","windDirDay":"东风","windScaleDay":"3-4","windSpeedDay":"16","wind360Night":"45","windDirNight":"东北风","windScaleNight":"1-2","windSpeedNight":"3","humidity":"57","precip":"0.0","pressure":"998","vis":"25","cloud":"5","uvIndex":"5"},
{"fxDate":"2021-05-03","sunrise":"05:37","sunset":"19:11","moonrise":"01:00","moonset":"11:05","moonPhase":"亏凸月","tempMax":"21","tempMin":"14","iconDay":"305","textDay":"小雨","iconNight":"101","textNight":"多云","wind360Day":"45","windDirDay":"东北风","windScaleDay":"1-2","windSpeedDay":"3","wind360Night":"315","windDirNight":"西北风","windScaleNight":"4-5","windSpeedNight":"34","humidity":"20","precip":"5.1","pressure":"996","vis":"25","cloud":"80","uvIndex":"3"},
{"fxDate":"2021-05-04","sunrise":"05:36","sunset":"19:12","moonrise":"01:46","moonset":"12:09","moonPhase":"下弦月","tempMax":"27","tempMin":"13","iconDay":"101","textDay":"多云","iconNight":"154","textNight":"阴","wind360Day":"315","windDirDay":"西北风","windScaleDay":"4-5","windSpeedDay":"34","wind360Night":"315","windDirNight":"西北风","windScaleNight":"3-4","windSpeedNight":"16","humidity":"27","precip":"0.0","pressure":"997","vis":"25","cloud":"0","uvIndex":"11"}
],
"refer":{"sources":["Weather China"],"license":["no commercial use"]}
}
3.编写 Python (在树莓派自带的 Thonny IDE上编译)
import requests
# 填入 请求
url = 'https://devapi.qweather.com/v7/weather/3d'
# 填入 地址 和 你的KEY
value = {
'location': '101180501',
'key': 'fed098f400e84c8a9f029bcb72c1197d',
# 'lang': 'zh'
}
sd = requests.get(url, params=value)
sds = sd.json()
#从网址内获取指定数据
#具体 关键词 详见官方 API 手册的解释
for i in range(3):
sdf = sds['daily'][i]['fxDate'] #date
Mo = '平顶山 ' + sdf
sdf = sds['daily'][i]['textDay'] #date
Mo = Mo + ' ' + sdf + '\n'
sdf = sds['daily'][i]['tempMax'] #Max tempurture
Mo = Mo + '最高温度: ' + sdf + '℃ '
sdf = sds['daily'][i]['tempMin'] #Mai tempurture
Mo = Mo + '最低温度: ' + sdf + '℃ ' + '\n'
sdf = sds['daily'][i]['windScaleNight']
Mo = Mo + '风力等级: ' + sdf + ' '
sdf = sds['daily'][i]['windDirDay']
Mo = Mo + '风向: ' + sdf + ' ' + '\n'
sdf = sds['daily'][i]['vis']
Mo = Mo + '能见度: ' + sdf + 'KM '
sdf = sds['daily'][i]['moonPhase']
Mo = Mo + ' 月相: ' + sdf
sdf = sds['daily'][i]['cloud']
Mo = Mo + ' 云量 : ' + sdf + ' % \n'
print(Mo)
- 编译结果 :
平顶山 2021-05-03 小雨
最高温度: 20℃ 最低温度: 14℃
风力等级: 4-5 风向: 东南风
能见度: 24KM 月相: 亏凸月 云量 : 64 %
平顶山 2021-05-04 阴
最高温度: 26℃ 最低温度: 11℃
风力等级: 3-4 风向: 西北风
能见度: 25KM 月相: 下弦月 云量 : 0 %
平顶山 2021-05-05 阴
最高温度: 25℃ 最低温度: 15℃
风力等级: 3-4 风向: 南风
能见度: 25KM 月相: 残月 云量 : 0 %
4.写在最后
1. 关键词 请务必按照官方 API 文档填写 , 否则会报错 : KeyError ....