【问题标题】:automatization of requests请求自动化
【发布时间】:2020-06-02 21:37:05
【问题描述】:

我需要在一段时间内从 Web 界面中提取 JSON 文件。注释的限制 - 100。JSON 文件由一些事件和时间戳组成,当事件发生时。如果响应太大,我会在文件中收到通知 - moreDataAvailable = True。要获取未包含在我已经获得的文件中的其他数据,我需要在 URL 中进行一些更改来发出新请求 - 更改最后一个事件的时间戳 (receivedDateTime) 将其增加 1 毫秒。

所以,我已经通过 Python 使用 requests.get() 编写了请求

>>> import requests
>>> response = requests.get(
...     'https://api.blablabla.com/event/eventstatuses?requestId=1234&datetype=received&starttime=2020-02-10T00%3A00%3A57.001Z&...'
... )
>>> json_response = response.json()
>>> print(json_response)

如何在请求期间自动提取所有数据的请求? (moreDataAvailable = False) 是否可以连接所有下载的 JSON,例如,在一个文件中以将其导出到数据仓库中?

【问题讨论】:

    标签: python json python-3.x rest python-requests


    【解决方案1】:

    您可以为此编写一个循环。

    import time
    timeout = time.time() + 60*5   # 5 minutes from now
    while True:
        sendRequest()
        if time.time() > timeout:
            break
        time.sleep(60)   # Delays for 60 seconds. You can also use a float value.
    
    
    def sendRequest():
      import requests
      response = requests.get(
        'https://api.blablabla.com/event/eventstatuses? 
          requestId=1234&datetype=received&starttime=2020-02- 
           10T00%3A00%3A57.001Z&...'
       )
      json_response = response.json()
      print(json_response)
    

    【讨论】:

      猜你喜欢
      • 2012-06-11
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      相关资源
      最近更新 更多