简体   繁体   English

Python API 使用嵌套 for 循环调用 cursor 分页,无法从循环内部检索字典键

[英]Python API call with nested for loop for cursor pagination, cannot retrieve dict keys from inside the loop

I am struggling to figure out how to implement cursor pagination from the API I am trying to query.我正在努力弄清楚如何从我试图查询的 API 中实现 cursor 分页。 I previously tried to declare the first cursor, and then run a while cursor is not None loop, but that gave me more errors than the for loop lol.我之前尝试声明第一个 cursor,然后运行一段时间 cursor is not None 循环,但这给了我比 for 循环更多的错误哈哈。

I divided by 100 for the number of pages, as the api documentation dictates the max limit from a singular api call is 100.我将页数除以 100,因为 api 文档规定单个 api 调用的最大限制为 100。

 import requests import json import pandas as pd url = "https://deep-index.moralis.io/api/v2/0xa081AE42802E0Be86f5A6dD9274ADf50C9986a1D/nft?chain=eth&format=decimal" headers = { "Accept": "application/json", "X-API-Key": "myapikey" } medRare = [] def get_total(): r1 = requests.get(url, headers = headers) r2 = json.loads(r1.text) global pages pages = round(r2['total']/100) get_total() for x in range(2): r1 = requests.get(url, headers = headers) r2 = json.loads(r1.text) r3 = r2['result'] for nfts in r3: medRare.append(nfts) cursor = r2['cursor'] url = url+"&cursor="+str(cursor)

The error I am getting is on the "r3 = r2['result']" line inside of the loop, KeyError: 'result'我得到的错误是在循环内的“r3 = r2 ['result']”行,KeyError:'result'

I am able to recreate this loop manually with no issues, but when I try to make it automatic with recursion it is giving me these errors.我可以毫无问题地手动重新创建此循环,但是当我尝试通过递归使其自动创建时,它给了我这些错误。 Any help is appreciated, TIA.任何帮助表示赞赏,TIA。

r2 results r2 结果

The problem may be caused by url = url+"&cursor="+str(cursor) .该问题可能是由url = url+"&cursor="+str(cursor)引起的。 This will overwrite the existing url instead of modifying the cursor string.这将覆盖现有的url而不是修改 cursor 字符串。 Let say the for loop iterates 3 times.假设for循环迭代 3 次。 The value of url will become url的值将变为

# first iteration
example.com&cursor=cursor1
# second iteration
example.com&cursor=cursor1&cursor=cursor2
# third iteration
example.com&cursor=cursor1&cursor=cursor2&cursor=cursor3

Thus, the second API call onwards will return unexpected result, eg r2['result'] may be equal to None .因此,第二个 API 调用将返回意外结果,例如r2['result']可能等于None You can try to use another variable name to prevent this from happening.您可以尝试使用另一个变量名来防止这种情况发生。

url = "example.com"
headers = {"Accept": "application/json","X-API-Key": "my-api-key"}
results = []

def get_total():
    r1 = requests.get(url, headers = headers)
    r2 = json.loads(r1.text)
    return round(r2['total']/100)

pages = get_total()

for x in range(1,pages):
    r1 = requests.get(url, headers = headers)
    r2 = json.loads(r1.text)
    r3 = r2['result']
    
    for i in r3:
        results.append(i)
        
    cursor = r2['cursor']

    full_url = url+"&cursor="+str(cursor)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM