说明:个人开发使用时想到获取天气看看情况,然后找到了高德的API,获取各个地区的天气情况
高德地图开发平台:/
点击天气服务API可以跳转到天气查询的API文档
第一步,申请”web服务 API”密钥(Key);
第二步,拼接HTTP请求URL,第一步申请的Key需作为必填参数一同发送;
第三步,接收HTTP请求返回的数据(JSON或XML格式),解析数据。
URL:
请求方式:GET
请求参数:key:你的应用key
city:查询城市的城市编码,下载城市编码文档
extensions:天气类型:base/all。base:返回实况天气;all:返回预报天气。根据具体情况选择。
import requestsurl = ''
params_realtime = {'key':'你的应用key','city':'360731', # 从城市编码里获取的a丢包code'extensions':'base' # 获取实时天气
}
params_estimate = {'key':'你的应用key','city':'360731','extensions':'all' #获取预报天气
}res = (url=url,params=params_estimate) # 预报天气
res2 = (url=url,params=params_realtime) # 实时天气
tianqi = res.json()
print(tianqi)
tianqi2 = res2.json()
print(tianqi2)# ('forecasts'))
# province = ('forecasts')[0].get("province") # 获取省份
province = tianqi['forecasts'][0]["province"] # 获取省份
city = ('forecasts')[0].get("city") # 获取城市
adcode = ('forecasts')[0].get("adcode") # 获取城市编码
reporttime = ('forecasts')[0].get("reporttime") # 获取发布数据时间
date = ('forecasts')[0].get("casts")[0].get('date') # 获取日期
week = ('forecasts')[0].get("casts")[0].get('week') # 获取星期几
dayweather = ('forecasts')[0].get("casts")[0].get('dayweather') # 白天天气现象
nightweather = ('forecasts')[0].get("casts")[0].get('nightweather') # 晚上天气现象
daytemp = ('forecasts')[0].get("casts")[0].get('daytemp') # 白天温度
nighttemp = ('forecasts')[0].get("casts")[0].get('nighttemp') # 晚上温度
daywind = ('forecasts')[0].get("casts")[0].get('daywind') # 白天风向
nightwind = ('forecasts')[0].get("casts")[0].get('nightwind') # 晚上风向
daypower = ('forecasts')[0].get("casts")[0].get('daypower') # 白天风力
nightpower = ('forecasts')[0].get("casts")[0].get('nightpower') # 晚上风力print("省份:",province)
print("城市:",city)
print("城市编码:",adcode)
print("发布数据时间:",reporttime)
print("日期:",reporttime)
print("星期:",week)
print("白天天气现象:",dayweather)
print("晚上天气现象:",nightweather)
print("白天温度:",daytemp)
print("晚上温度:",nighttemp)
print("白天风向:",daywind)
print("晚上风向:",nightwind)
print("白天风力:",daypower)
print("晚上风力:",nightpower)
本文发布于:2024-01-28 01:30:20,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063766263854.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |