天气天气预报实时天气空气质量 天气

OpenWeatherMap天气API

OpenWeatherMap是全球流行的天气数据服务,提供实时天气、5天/3小时预报、空气质量、地理编码与气象图等接口。免费套餐每月100万次调用、每分钟60次,注册即可获取密钥。

查看官方文档 ↗

接入教程

https://api.openweathermap.org/data/2.5/weather?q=Beijing&appid=你的KEY&units=metric&lang=zh_cn

1. 注册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并暂停调用。

相似接口推荐

Weatherstack

Weatherstack 是一款提供全球实时、历史和预测天气数据的API服务,以轻量级JSON格式返回准确的气象信息,支持数百万个地点查询。

免费
Open-Meteo天气API

Open-Meteo是一个免费开源天气API,无需Key与注册,提供30多个模型的预报、1940年以来的历史天气,以及空气质量、海洋与地理编码等产品。免费层每天10000次,限非商业用途。

免费
和风天气

和风天气为开发者提供全球天气数据服务,涵盖实时天气、7天预报、空气质量AQI、灾害预警、台风、潮汐、日出日落与生活指数等。免费订阅每天提供1000次请求额度。

免费增值
心知天气

心知天气提供中文天气数据API,覆盖实时天气、15天预报、预警与生活指数等。免费版为个人开发者提供每天500次请求。

免费增值