【问题标题】:How to read the next page on API using python?如何使用 python 阅读 API 的下一页?
【发布时间】:2017-12-30 19:10:44
【问题描述】:

我需要有关如何执行循环的帮助,因此每次我发出 GET 请求时,它始终是来自 API 的新页面。

我从获得第一个响应开始。它包含一个参数到下一页next_key

{
  "result": [
    {
      ...,
      ...
    }
  ],
  "next_key": 123
 }

以下是我目前的尝试

import requests
import json

url = "https://flespi.io/gw/channels/all/messages"
headers = {"Authorization": "FlespiToken 23ggh45"}

def getFirst():
    data = {"limit_count":100, "limit_size":10000}
    params = {"data":json.dumps(data, separators=(",", ":"))}

    reqFirst = requests.get(url, params=params, headers=headers).json()
    return reqFirst["next_key"] ## this returns "123"

def getDataNext():
    data = {"limit_count":100, "limit_size":10000, "curr_key":getFirst()}
    params = {"data":json.dumps(data, separators=(",", ":"))}

    reqNext = requests.get(url, params=params, headers=headers)

    jsonData = reqNext.json()

    while True:
        if "next_key" in jsonData:
            data = {"limit_count":100, "limit_size":10000,"curr_key":jsonData["next_key"]}
            params = {"data":json.dumps(data, separators=(",", ":"))}

            req = requests.get(url, params=params, headers=headers).json()  ## this should do GET request for the third page and so on...

            print req["next_key"] # this returns "3321" which is the value for "next_key" in second page

        else:
            pass

getDataNext()

包括limit count、limit size和curr key的完整url如下https://flespi.io/gw/channels/all/messages?data=%7B%22curr_key%22%123%2C%22limit_count%22%3A100%2C%22limit_size%22%3A10000%7D

如您所见,这只返回第二页 jsonData["next_key"]。我想做的是,对于每个 GET 请求,程序都会读取 next_key 并将其放在下一个 GET 请求中。

我正在考虑在curr_key 上使用增量,但密钥是随机的,而且我不知道有多少页。

我相信必须有一个简单的解决方案,但显然我无法考虑。感谢您的帮助和建议。

【问题讨论】:

  • 我们无法访问 API 或它的文档,我们应该如何提供建议?你的方法的实际问题是什么?
  • 您需要根据网站接受页码的方式动态创建 URL 或调整标题。有了更多信息,我们应该能够轻松地做到这一点。网站希望在哪里找到页码?可以分享一下网站吗?
  • 您好克劳斯,感谢您的评论。首先,我认为在这里提供对 API 或其文档的访问并不重要,因为我目前的方法在这里遇到的问题是我无法让它为下一页做 GET 请求。根据我目前的尝试,它只停在第二页。我将编辑我的问题以包含一些其他信息,例如 url
  • @Farmer Joe,我已经编辑了我的问题。虽然网址是真实的,但令牌仍然不是真实的,但我希望它可以提供帮助。谢谢

标签: python api pagination


【解决方案1】:

试试这个

has_next_key = False
nextKey = ""

if "next_key" in jsonData:
    has_next_key = True
    nextKey = jsonData["next_key"]

while has_next_key:
    data = {"limit_count":100, "limit_size":10000,"curr_key":nextKey}
    params = {"data":json.dumps(data, separators=(",", ":"))}

    req = requests.get(url, params=params, headers=headers).json()  ## this should do GET request for the third page and so on...
    if "next_key" in req:
        nextKey = req["next_key"]
        print nextKey # this returns "3321" which is the value for "next_key" in second page
    else:
        has_next_key = False
        # no next_key, stop the loop

【讨论】:

  • 谢谢。你的答案对我有用。你能解释一下你的答案和我的代码有什么区别吗?为什么我需要将if 块放在while 块之外?
  • 使用while True: 你正在做没有结束的循环,它用相同的next_key 重复请求,因为jsonData 永远不会获得新的价值,所以你永远不会到达第三页。 ifwhile 块中用于设置nextKey 并检查我们是否需要继续循环(has_next_key
猜你喜欢
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
相关资源
最近更新 更多