我正在尝试使用 Flask 应用程序中的以下代码获取未解决问题的数量。
g = Github()
repo = g.get_repo(repo_name)
open_pulls = repo.get_pulls(state='open')
open_pull_titles = [pull.title for pull in open_pulls]
open_issues = repo.get_issues(state='open')
open_issues = [issue for issue in open_issues if issue.title not in open_pull_titles]
我收到错误github.GithubException.RateLimitExceededException:
。
repo.get_issues()
返回打开的问题和拉取请求的数量。
如果我理解正确,get_issues 和 get_pulls 的返回类型是 PaginatedList。 它使用yield element
进行迭代。 所以直到open_issues = [issue for issue in open_issues if issue.title not in open_pull_titles]
才会执行请求。 如果达到您的令牌限制,它将抛出 RateLimitExceedException。
有什么解决方法吗?
g = Github()
你在这一步认证了吗? 公共 api 的速率限制较少。
g = Github()
你在这一步认证了吗? 公共 api 的速率限制较少。
是的,我确实验证了这一步。
@242jainabhi
我在达到速率限制时通常会做的事情只是将程序推迟一段时间。
我可能不使用列表推导式,而是仅使用带有 try-catch 的公共循环。 一旦捕获到限速异常,调用sleep函数等待一段时间,再次使用GitHub API检查限速。 只有当速率限制回到 5000 时,代码才会继续。
我很抱歉遗漏了一部分代码。 下面是第一个注释中代码的延续。
我正在访问所有未解决的问题的created_at
日期。 这将再次访问所有问题的 API,因此最终调用超过限制。
for issue in open_issues:
created_at = issue.created_at.timestamp()
我找不到这个问题的解决方案。 即使我对请求进行了身份验证,如果问题太多(假设为 2000),限制也会被耗尽。
像这样的东西:
repositories = g.search_repositories(
query='stars:>=10 fork:true language:python')
还会触发速率限制。 我想,它正在自动进行分页并触发速率限制? 有什么方法可以让我手动完成以便我可以暂停?
我现在意识到这是一个真正的问题。
一种可能的解决方法可能是如下所示的代码片段。 我还没有尝试过,让我知道它是否有效。
iter_obj=iter(open_issues) ## PaginatedList is a generator
while True:
try:
issue=next(iter_obj)
## do something
except StopIteration:
break # loop end
except github.GithubException.RateLimitExceededException:
sleep(3600) # sleep 1 hour
## check token limits
continue
@wangpeipei90不起作用。
出于某种原因,这种行为是高度不可预测的,而且令人发狂。
我的程序可以有效地循环并抢先调用 ratelimit api 以检查它是否在限制范围内保持一到两个小时,然后随机给出 403。
一些本地速率限制遵守将在这里受到热烈欢迎。 当您的应用程序在平稳运行两个小时后决定溢出 bean 时,必须根据直觉实现睡眠不应该是预期的行为。
File "word.py", line 126, in get_stargazers_inner
for i in repo.get_stargazers_with_dates():
File "/usr/local/lib/python3.6/dist-packages/github/PaginatedList.py", line 62, in __iter__
newElements = self._grow()
File "/usr/local/lib/python3.6/dist-packages/github/PaginatedList.py", line 74, in _grow
newElements = self._fetchNextPage()
File "/usr/local/lib/python3.6/dist-packages/github/PaginatedList.py", line 199, in _fetchNextPage
headers=self.__headers
File "/usr/local/lib/python3.6/dist-packages/github/Requester.py", line 276, in requestJsonAndCheck
return self.__check(*self.requestJson(verb, url, parameters, headers, input, self.__customConnection(url)))
File "/usr/local/lib/python3.6/dist-packages/github/Requester.py", line 287, in __check
raise self.__createException(status, responseHeaders, output)
github.GithubException.RateLimitExceededException: 403 {'message': 'API rate limit exceeded for user ID xxxx.', 'documentation_url': 'https://developer.github.com/v3/#rate-limiting'}
此外,可以使用退避库——但是它不能说明项目迭代中的当前位置,因此会重新从头开始。
好吧,我访问了https://github.com/settings/tokens并做了一个“重新生成令牌”。 这让我再次滚动,但我不确定多久。
我使用了“令牌”身份验证方法。 例子:
github = Github("19exxxxxxxxxxxxxxxxxxxxxe3ab065edae6470")
有关过度请求,另请参阅 #1233。
此问题已自动标记为陈旧,因为它最近没有活动。 如果没有进一步的活动发生,它将被关闭。 感谢你的贡献。
@王佩佩90
它对我有用,但在我使用的版本中RateLimitExceededException
不在GithubException
之下。 这是我的代码。
from github import RateLimitExceededException
issues = g.search_issues(query=keyword, **{'repo': repo, 'type': 'pr'})
iter_obj = iter(issues)
while True:
try:
pr = next(iter_obj)
with open(pr_file, 'a+') as f:
f.write(pr.html_url + '\n')
count += 1
logger.info(count)
except StopIteration:
break # loop end
except RateLimitExceededException:
search_rate_limit = g.get_rate_limit().search
logger.info('search remaining: {}'.format(search_rate_limit.remaining))
reset_timestamp = calendar.timegm(search_rate_limit.reset.timetuple())
# add 10 seconds to be sure the rate limit has been reset
sleep_time = reset_timestamp - calendar.timegm(time.gmtime()) + 10
time.sleep(sleep_time)
continue
这是日志的一部分:
2020/01/08 23:42:09 PM - INFO - search remaining: 0
谢谢, @Xiaoven我终于可以用你的代码解决这个问题了。
最有用的评论
如果我理解正确,get_issues 和 get_pulls 的返回类型是 PaginatedList。 它使用
yield element
进行迭代。 所以直到open_issues = [issue for issue in open_issues if issue.title not in open_pull_titles]
才会执行请求。 如果达到您的令牌限制,它将抛出 RateLimitExceedException。