系统、详细地介绍 GitHub 官方 API 的能力边界

一、GitHub 官方 API 概览

GitHub 提供两种主流 API:

类型 特点 适用场景
REST API v3 基于 HTTP 资源路径(如 /repos/{owner}/{repo}),简单直观 获取仓库、用户、Issue、PR、Star 等结构化数据
GraphQL API v4 单一端点 /graphql,客户端按需查询字段,减少请求次数 复杂嵌套查询(如"某用户最近 10 个仓库的 Star 数和最新 Issue")

🔐 认证方式 :Personal Access Token (PAT) 或 OAuth App Token

📈 速率限制

  • 未认证:60 次/小时
  • 认证后:5000 次/小时(REST),更高(GraphQL 按节点计费)

二、✅ 官方 API 支持的数据(无需爬虫)

1. 仓库(Repository)元数据

  • 端点GET /repos/{owner}/{repo}

  • 返回字段

    json 复制代码
    {
      "name": "react",
      "full_name": "facebook/react",
      "description": "A declarative, efficient...",
      "stargazers_count": 210000,
      "forks_count": 45000,
      "language": "JavaScript",
      "updated_at": "2026-01-08T12:34:56Z",
      "topics": ["ui", "javascript", "frontend"],
      "license": { "key": "mit" },
      "homepage": "https://react.dev"
    }

2. 搜索仓库(支持语言筛选 + 排序)

  • 端点GET /search/repositories

  • 示例 :获取 Python 语言中 Star 最多的项目

    复制代码
    GET https://api.github.com/search/repositories?q=language:python&sort=stars&order=desc&per_page=30
  • 支持筛选条件

    • language:python
    • stars:>1000
    • pushed:>2025-01-01
    • topic:machine-learning

✅ 这就是实现"最多 Star 项目排行榜"的标准方式。

3. Issues / Pull Requests

  • 列出仓库 Issues:GET /repos/{owner}/{repo}/issues
  • 获取单个 PR 详情:GET /repos/{owner}/{repo}/pulls/123
  • 支持状态、标签、作者等过滤

4. 用户信息 & 仓库列表

  • 用户资料:GET /users/octocat
  • 用户公开仓库:GET /users/octocat/repos?sort=updated

5. Commit 历史 & 文件内容

  • 获取分支最新 Commit:GET /repos/{owner}/{repo}/commits/main
  • 读取文件内容(Base64 编码):GET /repos/{owner}/{repo}/contents/README.md

6. Star 历史(间接支持)

虽然不能直接获取"每日新增 Star",但可通过:

  • GraphQL 查询 stargazers 并按时间排序(受限于分页)
  • 第三方服务(如 star-history)聚合

官方立场(来自 GitHub 文档与社区回应):

"Trending 页面是基于内部算法生成的,包含时间衰减、用户地理位置、活跃度等多种非公开因素,不作为稳定 API 对外提供。"

技术原因:

  • Trending 不是简单的"过去 24 小时 Star 最多"
  • 它经过反刷榜机制新项目加权多样性推荐等处理
  • GitHub 希望开发者使用 Search API + 自定义逻辑 构建榜单,而非依赖其内部排名

结果:

  • 无官方端点 (如 /trending/explore
  • 页面 HTML 是唯一公开来源

方案 A:使用可信的开源代理服务(✅ 推荐)

  • 原理 :定时爬取 github.com/trending 页面,解析为 JSON

  • API 示例

    bash 复制代码
    curl "https://ghapi.huchen.dev/repositories?language=python&since=daily"
  • 返回字段

    json 复制代码
    [
      {
        "author": "microsoft",
        "name": "TypeScript",
        "url": "https://github.com/microsoft/TypeScript",
        "description": "TypeScript is a superset of JavaScript...",
        "language": "TypeScript",
        "stars": 98765,
        "forks": 12345,
        "currentPeriodStars": "+1,234"  // 今日新增 Star
      }
    ]
  • 优点

    • 免去自己写爬虫
    • 已处理反爬(User-Agent、频率控制)
    • 支持 daily/weekly/monthly + 多语言

⚠️ 注意:该服务由社区维护,不要用于高频商业用途。可自行部署(Node.js + Cheerio)。


方案 B:自己写爬虫(⚠️ 谨慎使用)

基本步骤(Python 示例):
python 复制代码
import requests
from bs4 import BeautifulSoup

def get_trending(lang="python", since="daily"):
    url = f"https://github.com/trending/{lang}?since={since}"
    headers = {"User-Agent": "Mozilla/5.0 (compatible; TrendingBot/1.0)"}
    resp = requests.get(url, headers=headers)
    
    soup = BeautifulSoup(resp.text, 'lxml')
    repos = []
    for article in soup.select('article'):
        name = article.select_one('h2 a').text.strip().replace('\n', '').replace(' ', '')
        desc = article.select_one('p')?.text.strip() if article.select_one('p') else ''
        stars_today = article.select_one('[aria-label*="stars"]')?.text.strip()
        repos.append({"name": name, "description": desc, "stars_today": stars_today})
    return repos
风险与注意事项:
风险 应对措施
IP 被封 控制请求频率(≥5秒/次),使用代理池
页面结构变更 监控解析失败,设置 fallback
违反 ToS 仅用于个人/非商业用途,遵守 robots.txt

📜 GitHub robots.txt 允许爬取 /trending,但要求 合理速率


五、最佳实践建议

场景 推荐方案
开发个人项目排行榜 使用 ghapi.huchen.dev + GitHub Search API
企业级应用 自建 github-trending-api 服务 + 缓存(Redis)
学术研究 结合 Search API(官方) + Trending(代理)做对比分析
避免 直接高频爬取 GitHub 主站 HTML(易被限流)

六、总结

数据类型 是否有官方 API 获取方式
仓库详情、Star 数、语言、Issue、PR ✅ 有 REST/GraphQL API
搜索仓库(按语言/Star 排序) ✅ 有 /search/repositories
GitHub Trending(热榜) 第三方代理(如 ghapi.huchen.dev)或合规爬虫

💡 核心原则
能用官方 API 的,绝不爬页面;只有 Trending 这种例外,才考虑代理或爬虫。

相关推荐
KevinShi_BJ19 小时前
Github Copilot 实践
github·copilot
秋雨雁南飞19 小时前
图床软件 PicGo + Github
github·picgo·图床
moment&forever20 小时前
GitHub 托管 API 地址配置文件:实现零成本云配置托管
github
小龙1 天前
【Git 报错解决】本地无有效提交无法推送(`src refspec main does not match any`)
git·github·报错
行百里er1 天前
一个还没写代码的开源项目,我先来“画个饼”:Spring Insight
后端·开源·github
知行力1 天前
【GitHub每日速递 20260108】告别云服务弊端,Memos隐私至上自托管笔记服务来袭!
笔记·github
无限进步_2 天前
【数据结构&C语言】对称二叉树的递归之美:镜像世界的探索
c语言·开发语言·数据结构·c++·算法·github·visual studio
CoderJia程序员甲2 天前
GitHub 热榜项目 - 日榜(2026-1-7)
人工智能·ai·大模型·github·ai教程
逛逛GitHub2 天前
GitHub 上 2300 点赞的搜索 Agent,有点惊艳啊。
github