python - 如何获取 GitHub 仓库的贡献者总数?

标签 python python-requests github-api

如何获取GitHub仓库的贡献者总数?由于分页,API 使其变得相当困难。

这是我到目前为止使用 Python 尝试的结果:

contributors = "https://api.github.com/repos/JetBrains/kotlin-web-site/contributors"
x = requests.get(contributors)
y = json.loads(x.text)
len(y) # maximum 30 because of pagination

最佳答案

作为最后的手段,您可以从 GitHub HTML page 中获取所需的值(lxml.html lib 必填):

import requests
from lxml import html

r = requests.get('https://github.com/JetBrains/kotlin-web-site')
xpath = '//span[contains(@class, "num") and following-sibling::text()[normalize-space()="contributors"]]/text()'
contributors_number = int(html.fromstring(r.text).xpath(xpath)[0].strip())
print(contributors_number)
# 338

关于python - 如何获取 GitHub 仓库的贡献者总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53431480/

相关文章:

Python库请求打开错误的页面

java - 使用 Java API 从 GitHub 获取所有提交

python - 在python中用非字符串值替换子字符串

python - imageio 生成黑色图像序列

python - 括号 URL 调度程序 Django?

python - 循环中的选项值

python - 如何将列表中的行插入到QSqlTableModel中?

python - 如何替换python中特定单词下的值?

github - 通过 GitHub API 获取 Git 标签中的所有提交仅返回第一次提交

graphql - 如何查询 GitHub v4 API 以获取特定标签处的目录内容?