OpenWeatherMap API 是一个提供全球天气数据的平台,你可以通过这个 API 获取各种天气信息。以下是一个简单的教程,帮助你入门 OpenWeatherMap API。
获取 API 密钥
首先,你需要注册一个 OpenWeatherMap 账户,并获取一个 API 密钥。这个密钥将用于访问 API。
获取天气数据
使用以下 URL 获取天气数据:
https://api.openweathermap.org/data/2.5/weather?q=城市名&appid=你的API密钥
其中,城市名
是你想要查询的城市,你的API密钥
是你在 OpenWeatherMap 账户中获取的 API 密钥。
例如,查询北京的天气数据:
https://api.openweathermap.org/data/2.5/weather?q=北京&appid=你的API密钥
JSON 格式返回结果
OpenWeatherMap API 以 JSON 格式返回结果。以下是一个示例:
{
"weather": [
{
"id": 801,
"main": "Clear",
"description": "晴朗",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 282.15,
"feels_like": 282.15,
"temp_min": 282.15,
"temp_max": 282.15,
"pressure": 1013,
"humidity": 40,
"sea_level": 1013,
"grnd_level": 1013,
"temp_k": 282.15
},
"visibility": 10000,
"wind": {
"speed": 1.5,
"deg": 0
},
"clouds": {
"all": 1
},
"dt": 1637958600,
"sys": {
"type": 1,
"id": 1002,
"country": "CN",
"sunrise": 1637948561,
"sunset": 1637994949
},
"timezone": 28800,
"id": 1816670,
"name": "Beijing",
"cod": 200
}
温度单位
OpenWeatherMap API 默认使用开尔文(K)作为温度单位。如果你想使用摄氏度(°C)或华氏度(°F),可以在请求中添加参数 units
。
units=metric
:摄氏度units=imperial
:华氏度
例如,查询北京的天气数据,并使用摄氏度作为温度单位:
https://api.openweathermap.org/data/2.5/weather?q=北京&appid=你的API密钥&units=metric
更多信息
希望这个教程能帮助你入门 OpenWeatherMap API。如果你有其他问题,请随时提问。👍