【问题标题】:Weather Undground API call limit per minute每分钟 Weather Underground API 调用限制
【发布时间】:2018-06-01 03:12:31
【问题描述】:

我必须将我的 API 请求限制为每分钟 10 次调用,如何修改 for 循环来完成此操作?

我正在尝试在for observation 循环中添加time.sleep(8),但没有任何运气...有什么想法吗?

import arrow # learn more: https://python.org/pypi/arrow
from WunderWeather import weather # learn more: https://python.org/pypi/WunderWeather
import time

api_key = ''
extractor = weather.Extract(api_key)
zip = '53711'

# get 20170101 00:00
begin_date = arrow.get("2017","YYYY")
# get 20171231 23:00
end_date = arrow.get("2018","YYYY").shift(hours=-1)
for date in arrow.Arrow.range('hour',begin_date,end_date):
  # get date object for feature
  # http://wunderweather.readthedocs.io/en/latest/WunderWeather.html#WunderWeather.weather.Extract.date
  date_weather = extractor.date(zip,date.format('YYYYMMDD'))

  # use shortcut to get observations and data
  # http://wunderweather.readthedocs.io/en/latest/WunderWeather.html#WunderWeather.date.Observation
  for observation in date_weather.observations:
    time.sleep(8)
    print("Date:",observation.date_pretty)
    print("Temp:",observation.temp_f)

【问题讨论】:

    标签: python-3.x api weather weather-api weatherdata


    【解决方案1】:

    您仍然超出 API 限制的原因可能与您添加时间等待的行有关。如果您获得的 API 响应不包含任何观察结果,则内部循环将不会执行。因此,首先我会尝试在 API 调用之后立即在外循环中移动等待时间。

    您也可以考虑使用类似 loopingCall from twisted 的方法来安排您的任务每 X 秒运行一次 http://twistedmatrix.com/documents/9.0.0/core/howto/time.html

    【讨论】:

    • 我在 Jupiter notebooks 中做了一个!pip install twisted,底部有一个奇怪的错误,“扭曲的构建轮子失败”。您对如何安装twis​​ted有任何想法吗?像不用Pip直接安装wheel文件一样??
    • 我认为这正是我正在寻找的,如果我能让它工作......
    【解决方案2】:

    根据您希望数据的实时性或您可以承受落后一天的负担,您可以获得过去某个日期的所有观察结果,这将是一个 API 调用来检索一天的数据(或者它可能是一个当天观察结果的日终总结)。

    或者,如果您尝试每隔 x 分钟左右(低于限制)获取当前天气 我会使用某种带有计时器的循环(或者可能是扭曲的,这似乎抽象了“循环”),但会调用以下其中一个(取决于您要查找的内容)。您当前的代码正在查找过去的日期,但这些其他端点是当前日期。

    您不希望在观察循环中使用计时器,因为如上所述,可能没有计时器。

    http://wunderweather.readthedocs.io/en/latest/WunderWeather.html#WunderWeather.weather.Extract.hourly_daycast

    http://wunderweather.readthedocs.io/en/latest/WunderWeather.html#WunderWeather.weather.Extract.today_now

    可以调用类似下面的例子 http://wunderweather.readthedocs.io/en/latest/index.html#additional-examples

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多