在 GitHub 搜索 API 中按评论数量过滤

问题描述 投票:0回答:1

我正在尝试从 github tensorflow 存储库中提取问题,该问题有超过 250 条评论。然而,我总是收到 30 个结果(根据 GitHub 分页),而我应该只收到 2 个结果。 这是我的代码:

owner = "tensorflow"
repo = "tensorflow"
n_comments = 250
url = f"https://api.github.com/repos/{owner}/{repo}/issues"

token = "mytokenhere"

params = {
    'sort':'comments-desc',
    'q': 'comments:>{n_comments}',
}

headers = {
    "Authorization": f"token {token}",
    "Accept": "application/vnd.github.v3+json" # specifying json-formatted data
}

response = requests.get(url, headers = headers, params = params)

issues_with_sufficient_comments = []

if response.status_code == 200:
    # Extract the JSON data
    data = response.json()
    
    for item in data:
        issues_with_sufficient_comments.append(item['number'])
        print(item['number'])
        print(item['comments'])
    
else:
    # Print error message if request failed
    print(f"Error: {response.status_code}")

我也尝试过 url = f"https://api.github.com/repos/{owner}/{repo}/issues?q=comments:>250",这也没有帮助

python github-api
1个回答
0
投票
  • 尝试以下 api 调用来列出按评论数排序的问题。我看到的大多数评论都是
    133
https://api.github.com/repos/tensorflow/tensorflow/issues?sort=comments&direction=desc&per_page=100
  • 而且我没有看到任何评论数量的过滤参数。
  • 我建议遵循适合您的用例的工作流程。
1. get all issues
2. filter by number of comments you wants (comments > n)
3. process

1. Use sort and direction parameter.
2. 
   -if all response issue are > n_required_comments.
      - get new page
      - repeat 2
   - else
     - filter any issue with  < n_required_comments from last api call.
     - return

  • 两种方法都是有效的,第二种方法在某些情况下会减少 api 调用的次数。
© www.soinside.com 2019 - 2024. All rights reserved.