如何获得对GitHub存储库的贡献者总数?

时间:2018-11-22 12:52:34

标签: 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

1 个答案:

答案 0 :(得分:1)

万不得已的话,您可以从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