首页 > 解决方案 > python等待并重试当前迭代

问题描述

感谢您抽时间阅读。

我从 GitHub API 迭代地提取数据以获取一些存储库的名称和组织

for i in repoID:  #get names of repos
    response= requests.get('https://api.github.com/repositories/{}'.format(i),headers=headers)
    print(i)
    if 'full_name' in response.json():
        repokey[str(i)]=response.json()['full_name']
    else : 
        print('ratelimit reached for {}, waiting 120 sec'.format(i))
        time.sleep(120)
        continue

在继续下一次迭代之前,它会等待一定的时间。但我想在等待后重试达到 API 速率限制的当前迭代。

有没有办法可以做到这一点?

标签: python

解决方案


使用 awhile而不是遍历组件本身,这样您就可以返回。

i = 0
while i < len(repoID):
    previously_i = repoID[i]
    # your stuff
    i+=1

    if limit:
        i-=1

推荐阅读