GitHub API拉取TensorFlow高评论数Issues返回结果异常问题
问题原因及解决办法
你的问题出在用错了GitHub API的端点:/repos/{owner}/{repo}/issues这个接口不支持通过q参数过滤Issue,它的sort参数只能控制排序规则,但不会帮你筛选符合评论数条件的结果。所以你看到的30条只是按评论数降序排列的前30条Issue,根本没应用comments:>250的过滤规则。
要实现过滤需求,得改用GitHub的搜索API端点:https://api.github.com/search/issues,这个接口才支持q参数进行复杂筛选。
以下是修正后的代码:
import requests owner = "tensorflow" repo = "tensorflow" n_comments = 250 # 替换为搜索API的端点 url = "https://api.github.com/search/issues" token = "mytokenhere" params = { # q参数需要组合仓库范围、评论数条件,以及指定只搜索Issue(排除PR) 'q': f'repo:{owner}/{repo} comments:>{n_comments} is:issue', 'sort': 'comments', 'order': 'desc' } headers = { "Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json" } response = requests.get(url, headers=headers, params=params) issues_with_sufficient_comments = [] if response.status_code == 200: data = response.json() # 搜索API的结果在items字段里 for item in data['items']: issues_with_sufficient_comments.append(item['number']) print(item['number']) print(item['comments']) else: print(f"Error: {response.status_code}")
关键修改点说明:
- 端点换成
search/issues,这是唯一支持q过滤参数的接口。 q参数组合了三个条件:repo:{owner}/{repo}:限定搜索指定仓库comments:>{n_comments}:筛选评论数超过指定值的内容is:issue:确保只返回Issue,排除Pull Request(因为默认搜索结果包含PR)
- 搜索API的返回结构和
/repos/.../issues不同,结果列表在items字段下,需要通过data['items']访问。
这样修改后,就能得到你预期的、评论数超过250的Issue了。
内容的提问来源于stack exchange,提问作者kittielover
相关产品推荐
相关产品推荐

