[英]How to write a python loop to change a value for a dictionary key in API request?
I am writing an API request that gives paginated results.我正在编写一个提供分页结果的 API 请求。 To get results from the next page I need to take a value of 'next_page_cursor' and put it in the parameters of my request that is a dictionary.
要从下一页获得结果,我需要取值 'next_page_cursor' 并将其放入我的请求参数中,即字典。
This is what I have tried so far.这是我到目前为止所尝试的。 Need to keep changing cursor value in params until there are no more pages.
需要不断更改params 中的游标值,直到没有更多页面。
params = {'title': 'Cybertruck',
'per_page':100,
'cursor': '*'
}
response = requests.get("https://api.aylien.com/news/stories",
headers = headers, params=params).json()
if "next_page_cursor" in response:
cursor = response["next_page_cursor"]
You can use a while
loop:您可以使用
while
循环:
params = {
"title": "Cybertruck",
"per_page": 100,
"cursor": "initial_cursor"
}
def make_request(params)
return requests.get("https://api.aylien.com/news/stories",
headers=headers, params=params).json()
result = []
response = make_request(params)
while "next_page_cursor" in response:
params["cursor"] = response["next_page_cursor"]
response = make_request(params)
result.append(response["information_your_are_interested_in"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.