【问题标题】:Dark Sky API Iterate daily through one year in Python 3Dark Sky API 在 Python 3 中每天迭代一年
【发布时间】:2017-11-27 04:19:09
【问题描述】:

我只是尝试获取某个时间范围内的天气数据。 我想获得一整年的每日或每小时数据。 我刚刚尝试了以下代码:

from forecastiopy import *
from datetime import date, datetime, timedelta

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)

start_date = date(2015, 1, 1)
end_date = date(2015, 12, 31)
for single_date in daterange(start_date, end_date):
    time = single_date.strftime("%Y-%m-%d")
    print('DATE: ', time)


city = [40.730610, -73.935242]

fio = ForecastIO.ForecastIO(apikey,
                            units=ForecastIO.ForecastIO.UNITS_SI,
                            lang=ForecastIO.ForecastIO.LANG_ENGLISH,
                            latitude=city[0], longitude=city[1])

print('Latitude:', fio.latitude, 'Longitude:', fio.longitude)
print('Timezone', fio.timezone, 'Offset', fio.offset)
print(fio.get_url()) # You might want to see the request url

if fio.has_hourly() is True:
    hourly = FIOHourly.FIOHourly(fio)
    print('Hourly')
    print('Summary:', hourly.summary)
    print('Icon:', hourly.icon)

for hour in range(0, hourly.hours()):
    print('Hour', hour+1)
    for item in hourly.get_hour(hour).keys():
        print(item + ' : ' + str(hourly.get_hour(hour)[item]))
        # Or access attributes directly for a given minute.
        print(hourly.hour_5_time)
else:
    print('No Hourly data')

我明白了:

DATUM:  2015-01-01
DATUM:  2015-01-02
DATUM:  2015-01-03
...
DATUM:  2015-12-29
DATUM:  2015-12-30
Latitude: 40.73061 Longitude: -73.935242
Timezone America/New_York Offset -4
Hourly
Summary: Light rain starting this afternoon.
Icon: rain
Hour 1
visibility : 16.09
humidity : 0.52
...
Hour 49
visibility : 16.09
humidity : 0.57
apparentTemperature : 23.52
icon : partly-cloudy-day
precipProbability : 0
windGust : 2.7
uvIndex : 2
time : 1498395600
precipIntensity : 0
windSpeed : 2.07
pressure : 1014.84
summary : Mostly Cloudy
windBearing : 37
temperature : 23.34
ozone : 308.33
cloudCover : 0.65
dewPoint : 14.43
1498237200

我如何使用特定年份的每一天的时间参数来获得 365 每日报告或 365 * 24 小时报告?我不是python专家。

【问题讨论】:

  • 我在同一条船上,我知道您必须在 API 调用中包含日期。使用上面的每日和每小时调用只是请求当前日期的天气,它会自动调用 1 - 49 小时。您需要找到一种方法来调用 API 并循环遍历每一天。

标签: python-3.x api loops date iteration


【解决方案1】:

本博客提供了一些代码来查询日期https://nipunbatra.github.io/blog/2013/download_weather.html

times = []
data = {}
for attr in attributes:
    data[attr] = []

start = datetime.datetime(2015, 1, 1)
for offset in range(1, 60):
    forecast = forecastio.load_forecast(api_key, lat, lng, time=start+datetime.timedelta(offset), units="us")
    h = forecast.hourly()
    d = h.data
    for p in d:
        times.append(p.time)
        try:
            for i in attributes:
                data[i].append(p.d[i])
        except:
            print(KeyError)

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

它在 python 3.6 上适用于我...但是,当我查询 2019 年 3 月左右的日期以获取我的坐标时,我收到一个错误 KeyError: 'temperature'... 所以在这段代码中,我在 for 中添加了 try catach 错误p in d 循环

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 2020-07-15
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多