【问题标题】:Github API, fetch the top most starred public repositories that is written in Python languageGithub API,获取用 Python 语言编写的最高星级公共存储库
【发布时间】:2019-05-03 00:26:45
【问题描述】:

我正在尝试使用 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 个存储库中获得相同的结果? 我感谢所有有价值的信息。

【问题讨论】:

  • 纯代码编写请求在 Stack Overflow 上是题外话——我们希望这里的问题与特定编程问题有关——但我们很乐意帮助您自己编写!告诉我们what you've tried,以及您遇到的问题。这也将有助于我们更好地回答您的问题。
  • 谢谢肖恩。我会尽快更新我的问题。

标签: python pandas api github


【解决方案1】:

你可以使用github api的created参数。因此,要获得自第 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

祝你的项目好运!

【讨论】:

  • 我的荣幸!如果它解决了您的问题,请随时将其标记为答案
  • 我还有一个小问题:我的结果以这种格式显示:'URL': u'github.com/santinic/pampy', 'repository_ID': 156706494, 'number_of_stars': 1304, 'name': u'pampy','created_date':u'2018-11-08T12:51:17Z'。我怎么能把一个分隔符 |在每个值之后,而不是 ,
  • 你得到的只是python字典的标准方式。如果你打印你的 df,你会看到你得到不同的格式。
  • 小气道歉哈哈。我期待的是:{'URL': u'github.com/santinic/pampy'|'repository_ID': 156706494|'number_of_stars': 1304| '名字':你'pampy'| 'created_date': u'2018-11-08T12:51:17Z'}。我可以更改我的代码中的某些内容以获得结果吗:) 非常感谢您,很抱歉打扰
  • 您必须编写一个函数来用这样的字符串转换字典。不过,采用这种格式似乎有点奇怪。我不建议这样做。如果你真的想这样做,你可以这样做:json.dumps(d_temp).replace(',', '|')
猜你喜欢
  • 2017-07-29
  • 1970-01-01
  • 2013-12-11
  • 1970-01-01
  • 2018-10-07
  • 2016-12-14
  • 2020-07-10
  • 1970-01-01
  • 2022-12-10
相关资源
最近更新 更多