You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取2022年9月后创建的按星数排序的GitHub Python仓库列表

获取2022年9月后创建的热门Python仓库(GitHub程序化实现)

实现思路

  • 核心使用GitHub Search Repositories API,这是唯一支持批量筛选仓库(按语言、创建时间)和排序的官方接口,单个仓库API仅适用于查询特定仓库,无法满足批量需求。
  • 构造搜索条件:通过查询字符串指定language:python created:>=2022-09-01,同时设置排序参数为stars、排序方向为desc(降序)。
  • 处理分页机制:GitHub Search API默认每页返回30条结果,最多可设置为100条,需通过循环递增page参数来获取所有符合条件的仓库。
  • 规避速率限制:未授权请求每小时仅允许60次调用,建议使用GitHub个人访问令牌(PAT),将其加入请求头后可提升至每小时5000次调用,避免请求被拦截。

代码片段(Python实现)

import requests

def fetch_popular_python_repos(since_date="2022-09-01", per_page=100):
    # 替换为你的GitHub个人访问令牌,若无则注释掉headers中的Authorization字段
    headers = {
        "Authorization": "token YOUR_GITHUB_PAT",
        "Accept": "application/vnd.github.v3+json"
    }
    repos = []
    page = 1
    while True:
        url = f"https://api.github.com/search/repositories?q=language:python+created:>={since_date}&sort=stars&order=desc&per_page={per_page}&page={page}"
        response = requests.get(url, headers=headers)
        
        # 检查请求是否成功
        if response.status_code != 200:
            print(f"请求失败,状态码:{response.status_code},响应内容:{response.text}")
            break
        
        data = response.json()
        repos.extend(data["items"])
        
        # 检查是否还有下一页
        if len(data["items"]) < per_page:
            break
        page += 1
    
    # 按星数再次排序(API返回已排序,此处为冗余保障)
    repos_sorted = sorted(repos, key=lambda x: x["stargazers_count"], reverse=True)
    
    # 输出结果示例:仅展示关键信息
    for repo in repos_sorted[:20]:
        print(f"仓库名: {repo['full_name']} | 星数: {repo['stargazers_count']} | 创建时间: {repo['created_at']}")
    
    return repos_sorted

if __name__ == "__main__":
    fetch_popular_python_repos()

注意事项

  • 若未使用个人访问令牌,需注意每小时60次的调用限制,避免短时间内频繁请求。
  • GitHub Search API最多返回1000条结果,若需获取更多数据,需调整查询条件(比如按月份分段查询)。
  • 返回的items中包含仓库的完整信息,可根据需求提取更多字段(如克隆地址、fork数等)。

内容的提问来源于stack exchange,提问作者Exploring

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 18:45:08