多种免费天气api
多种免费天气api推荐
- 一、高德天气
- 二、格点天气
- 三、香港天文台
一、高德天气
api说明文档:https://lbs.amap.com/api/webservice/guide/api/weatherinfo
实例代码:
import requests# 香港天文台API的URL
api_url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"# 请求参数,指定获取METAR报告
params = {"key": "e094cccaa9f94c734344ab015d8b4ec6","city": "810018","extensions":"base","output":"JSON"
}# 发送HTTP GET请求获取数据
response = requests.get(api_url, params=params)
data = response.json()# 提取并打印METAR报告
print(data)
二、格点天气
api说明文档:https://dev.qweather.com/docs/api/grid-weather/
实例代码:
import requests# 替换为您自己的API密钥
api_key = '9618d24f770f4d4e844f6a2e93d0865f'# 目标API的URL
url = f'https://devapi.qweather.com/v7/grid-weather/now?location=113.93,22.31&key={api_key}'# 发送GET请求
response = requests.get(url)# 检查是否成功获取数据
if response.status_code == 200:data = response.json()# 在这里可以处理获取到的数据print(data["now"]["text"])
else:print(f"请求失败,状态码:{response.status_code}")
三、香港天文台
api说明文档:https://data.weather.gov.hk/weatherAPI/doc/HKO_Open_Data_API_Documentation_tc.pdf
注意请更换key和经纬度,多人使用相同key会被禁掉