【问题标题】:Is there a free historic weather data API with Latitude / Longitude support?是否有支持纬度/经度的免费历史天气数据 API?
【发布时间】:2019-04-10 17:13:40
【问题描述】:

我想通过纬度/经度提取某些国家(更具体地说是某些国家的省/州)的(免费)历史天气数据,我需要将结果作为 .csv 文件或 pandas 数据框。我尝试了 forecast.io / DarkSky (https://zeevgilovitz.com/python-forecast.io/) 的包装器,但它限制为每天 1000 个请求。 所以我想知道是否有任何 API 是免费的,可以返回 pandas 数据帧或 .csv 格式并支持经度/经度请求?

这是我尝试过的方法(如果您每天只需要 1000 个请求,也可以使用)。

lat = 30
lng = 5
start_date = datetime.datetime(2016, 1, 1)
attributes = ["temperature", "humidity", "pressure", "windSpeed"]


def getWeatherData(lat, lng, start_date, attributes):

    times = []
    data = {}

    for attr in attributes:
        data[attr] = []

    for offset in range(1, 1000):
        forecast = forecastio.load_forecast(api_key, lat, lng, time=start_date+datetime.timedelta(offset), units="us")
        h = forecast.hourly()
        d = h.data

        for p in d:
            times.append(p.time)
            for attr in attributes:
                data[attr].append(p.d[attr])

    weather_data = pd.DataFrame(data, index=times)

    return weather_data

【问题讨论】:

  • csv/df 格式是硬性要求,还是只需要 Pandas 可以读取的内容?

标签: api weather weather-api weatherdata


【解决方案1】:

https://openweathermap.org/price 有一个带有一些免费计划的 API,如果您每天需要

它不会返回 csv 或 DataFrame 格式的数据,但 pandas 可以处理它返回的 json 格式

例子:

import requests
import json
import pandas as pd

url = 'https://api.openweathermap.org/data/2.5/onecall?lat=33.44&lon=-94.04&exclude=hourly,minutely&appid=<api_key_hidden>'
data = requests.get(url).json()

df = pd.DataFrame.from_dict(data['daily'])

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 2019-02-16
    • 2018-05-27
    相关资源
    最近更新 更多