我正在尝试使用Python。我想要实现的是使用Github API,我想获取使用Python语言编写的,自上个月以来创建的十个最受关注的公共存储库。谁能给我提示我如何实现的?
到目前为止,我已经实现了以下目标:
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)}
我坚持的是: 如何在最近两个月和十大存储库中获得相同的结果? 我感谢所有有价值的信息。
答案 0 :(得分:1)
您可以使用github api的created
参数。因此,要获取自9月起按星标排序的python仓库,您可以执行以下请求。
https://api.github.com/search/repositories?q=created:">2018-09-30"language:python&sort=stars&order=desc
然后获得您可以做的十大仓库:
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
祝您项目顺利!