接入教程
https://api.openweathermap.org/data/2.5/weather?q=Beijing&appid=你的KEY&units=metric&lang=zh_cn1. 注册OpenWeatherMap账号,进入API Keys页面生成密钥。
2. 免费计划支持实时天气、5天预报与空气质量,每月100万次调用。
3. 按城市名查询实时天气:
4. 5天/3小时预报接口为 /data/2.5/forecast。
5. 免费数据约每2小时更新一次,适合非实时场景。
Python 获取实时天气
python
import requests
url = "https://api.openweathermap.org/data/2.5/weather"
params = {"q":"Beijing","appid":"你的KEY","units":"metric","lang":"zh_cn"}
r = requests.get(url, params=params)
data = r.json()
print(data["main"]["temp"], data["weather"][0]["description"])
PHP 获取实时天气
php
<?php
$url = "https://api.openweathermap.org/data/2.5/weather?q=Beijing&appid=你的KEY&units=metric&lang=zh_cn";
$d = json_decode(file_get_contents($url), true);
echo $d["main"]["temp"] . "C " . $d["weather"][0]["description"];
JavaScript 获取实时天气
javascript
const url = "https://api.openweathermap.org/data/2.5/weather?q=Beijing&appid=你的KEY&units=metric&lang=zh_cn";
const data = await fetch(url).then(r => r.json());
console.log(data.main.temp, data.weather[0].description);
常见问题
免费计划额度是多少?
免费计划每月100万次调用、每分钟60次,覆盖实时天气、5天/3小时预报与空气质量。
需要绑定信用卡吗?
不需要。注册邮箱即可免费获取API密钥,超出限额会返回429并暂停调用。
Aitishiku.com