首页 > 解决方案 > Github API,获取用 Python 语言编写的最高星级公共存储库

问题描述

我正在尝试使用 Python。我想要实现的是使用 Github API,我想获取使用 Python 语言编写并自上个月创建的前 10 个最受欢迎的公共存储库。谁能给我一些关于如何实现这一目标的提示?

到目前为止,我已经成功实现了以下目标:

import pandas as pd
import requests 
from datetime import datetime
df = pd.DataFrame(columns=['repository_ID', 'name', 'URL', 'created_date',  'description', 'number_of_stars'])
results = requests.get('https://api.github.com/search/repositories?q=language:python&sort=stars&order=desc').json()

for repo in results['items']:
        d_tmp = {'repository_ID': repo['id'],
                'name': repo['name'],
                'URL': repo['html_url'],
                'created_date': datetime.strptime(repo['created_at'], '%Y-%m-%dT%H:%M:%SZ'),


                'number_of_stars': repo['stargazers_count']}
        df = df.append(d_tmp, ignore_index=True)


print d_tmp

这给了我以下按星数降序排序的观看次数最多的结果:

{'URL': u'https://github.com/faif/python-patterns', 'repository_ID': 4578002, 'number_of_stars': 18103, 'name': u'python-patterns', 'created_date': datetime.datetime(2012, 6, 6, 21, 2, 35)}

我坚持的是: 如何在过去两个月和前 10 个存储库中获得相同的结果? 我感谢所有有价值的信息。

标签: pythonpandasapigithub

解决方案


您可以使用createdgithub api 的参数。因此,要获取自第 9 个月以来按星号排序的 python 存储库,您可以执行以下请求。

https://api.github.com/search/repositories?q=created:">2018-09-30"language:python&sort=stars&order=desc

然后要获得前 10 个回购协议,您可以执行以下操作:

top_ten = results['items'][0:10]

如果要限制 api 调用返回的项目数,可以使用per_page=10参数。下面的查询与上面的相同,但只返回 10 个结果。

https://api.github.com/search/repositories?q=created:">2018-09-30"language:python&sort=stars&order=desc&per_page=10

祝你的项目好运!


推荐阅读