如何通过天气API获取降水量/降雨量?

4 投票
2 回答
8033 浏览
提问于 2025-04-18 14:18

我试过了Open Weather Map,因为它的文档上说有“雨”,但我调用的时候却没有。所以我又试了Python Weather API,但是从weather.com、noaa或者yahoo天气那边的选项都没有降雨或降水的信息。接着我又尝试了Wunderground,但它似乎只适用于美国的城市,而且我也不想花钱去买一个API密钥。

有没有人知道我该怎么做?

在Open Weather Map上说有雨,但我在结果中却没有看到:

这是一个JSON调用的例子

{"coord":{"lon":139,"lat":35},
"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
"wind":{"speed":7.31,"deg":187.002},
"rain":{"3h":0},                      # on this line
"clouds":{"all":92},
"dt":1369824698,
"id":1851632,
"name":"Shuzenji", 
"cod":200}

但是,当我这样调用的时候

from pprint import pprint
import requests
r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=Vancouver')
pprint(r.json())

我得到的结果里没有降雨或降水的信息。

{u'base': u'cmc stations',
 u'clouds': {u'all': 0},
 u'cod': 200,
 u'coord': {u'lat': 49.25, u'lon': -123.12},
 u'dt': 1406042326,
 u'id': 6173331,
 u'main': {u'humidity': 77,
           u'pressure': 862,
           u'temp': 289.33,
           u'temp_max': 290.93,
           u'temp_min': 288.15},
 u'name': u'Vancouver',
 u'sys': {u'country': u'CA',
          u'message': 0.1867,
          u'sunrise': 1406032353,
          u'sunset': 1406088323},
 u'weather': [{u'description': u'Sky is Clear',
               u'icon': u'01d',
               u'id': 800,
               u'main': u'Clear'}],
 u'wind': {u'deg': 104.001, u'speed': 2.75}}

2 个回答

2

我找到了雨;)这可不容易,但我成功了。它确实存在。

    fetch(https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey})
  .then(res => res.json())
  .then(data => {
    console.log(data);
    const { pressure, temp, humidity } = data.main;
    const { speed, deg } = data.wind;
    const { description } = data.weather[0];
    const rain = data.rain['1h'];
    console.log(rain)
  });
3

根据文档weatherrain.3hsnow.3h都是可选参数,这意味着它们不一定会出现在结果中。

我理解这意味着,如果在查询的时间没有下雨或下雪,那么就不会报告雨和雪的情况,比如你提到的例子中说“天空晴朗”——但也有可能是说他们不保证会提供雨雪的数据。

撰写回答