【发布时间】:2020-12-19 09:39:14
【问题描述】:
我正在尝试为每个“切片”收集 250 条记录。我总共有 1429 条记录,所以 6 片 250 应该足以收集我所有的记录。我尝试使用以下代码执行此操作,但是当我运行它时,它只返回记录 251 到 500。我的预期结果是收到记录 1 到 1429。有人可以告诉我我做错了什么吗?
import requests
import json
import math
res = r.json()
token = res['access_token']
headers = {'Authorization': 'Bearer ' + token}
proxies = {'https': 'proxy.***.***.com:8080'}
mod_date = 'ModifiedOn gt 2020-01-31'
col = 'Field1, Field2, Field3, Field4'
params1 = (('$count', 'true'),)
response1 = requests.get('https://***-***-***.***.nl/odata/***', headers=headers, params=params1, proxies=proxies)
data = response1.json()
next_link = data['@odata.nextLink'] #<--- this is my 'next_link' link.
total_records = data['@odata.count'] #<--- this are the total records '1429'
records_per_page = next_link[313:] #<--- this are the records per page '250'
total_pages = total_records / int(records_per_page) #<--- these are the total pages '5.7'
list_to_store_all_sclices = []
list_pages = [i for i in range(1, math.ceil(total_pages) +1)] #<--- '5,7' is rounded to 6 and placed in list.
for x in list_pages:
response2 = requests.get(next_link.format(x), headers=headers, proxies=proxies)
data = response2.json()
list_to_store_all_sclices.append(data)
print(list_to_store_all_sclices)
【问题讨论】:
-
next_link.format(x)暗示next_link中有{},我对此表示怀疑。 -
@Chillie 即使我
remove .format(x)我仍然只得到251到500的记录 -
这正是我的观点。您实际上并没有请求任何新页面。由于您没有更改
next_link,因此您一直在请求第一个响应中的相同链接。 -
如何更改?我建议它自动继续使用 next_link。
标签: python loops pagination python-requests