首页 > 解决方案 > 如何获取 GitHub 存储库的贡献者总数?

问题描述

如何获取 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

标签: pythonpython-requestsgithub-api

解决方案


作为最后的手段,您可以从GitHub HTML 页面(需要lxml.html 库)中抓取所需的值:

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

推荐阅读