![](/img/trans.png)
[英]Referencing a variable showing 'local variable 'final_ans' referred before declaration'
[英]Issue with referencing local variable before declaration for API GET call
我在理解如何為我的 API 調用返回 next_page 令牌時遇到問題。 它包含 100 條記錄,並使用 next_page 令牌一次返回 30 條以獲取下一個 30...我能夠獲得初始數據集,在我的實例中,我只是嘗試返回前 3 頁的 next_page 令牌但到目前為止,它只是返回相同的 3。
def main_request(baseUrl, header, size):
repeat_count = 3
while repeat_count != 0:
response = requests.get(baseUrl + f'?page_size={size}' + f"&next_page_token={page_token}", headers=header)
api_data = response.json()
page_token = api_data['next_page_token']
print('Current Token: ', page_token)
repeat_count -= 1
在上面它不會運行,因為 page_token 尚未聲明,但如果我在開始時聲明它並將其設置為空字符串,它會運行但從第一頁返回相同的 3 個標記。
JSON 響應,如果我設置 page_token = ''
Current Token: TY3fkmCPZJkI4PdufKrdxlC6cblJHKZnnJ2
Current Token: TY3fkmCPZJkI4PdufKrdxlC6cblJHKZnnJ2
Current Token: TY3fkmCPZJkI4PdufKrdxlC6cblJHKZnnJ2
從有效負載中省略next_page
開始,然后在每個響應之后添加它。
def main_request(baseUrl, header, size):
payload = {'page_size': size}
for _ in range(3):
response = requests.get(baseUrl, data=payload, headers=header)
api_data = response.json()
page_token = api_data['next_page_token']
# Adds the first time, updates the others
payload['next_page_token'] = page_token
print('Current Token: ', page_token)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.