【发布时间】:2019-04-17 05:53:57
【问题描述】:
所以我正在努力使用 Python3 从 API 获取所有项目,问题是当我运行脚本时它只显示 100 ,而我知道它们超过 1000。
我尝试使用 while 循环,但重复相同的结果失败了
import requests
import json
def get_list_call(offset):
url = "https://link.com/monitoring/v1/switches"
querystring = {"limit":"%s"%limit,"offset":"%s"% offset}
headers = {
'Authorization': "Bearer Token",
'cache-control': "no-cache",
}
response = requests.request("GET", url, headers=headers,params=querystring)
return(response.text)
result = get_list_call(0)
data_json = json.loads(result)
count = (data_json['count'])
print (count)
我看到了 20 个项目的结果 和计数 = 20
【问题讨论】:
-
在发出请求之前,您似乎没有将查询字符串附加到 URL 中?
-
谢谢,,,我编辑了这个问题,问题是每次我运行它时,我都会得到相同的列表,即 100 个项目(开关),这就是为什么我将它命名为(get_list_call)函数来循环它并获得更多,,,我最终得到了同一个列表的无限循环。
-
查找 API 的文档。它们可能限制为最多 100 个,然后使用某种分页功能来允许您请求下一批。