Skip to content
On this page

GitHub API 用户信息获取教程

什么是 GitHub API

xxxxxxxxxx 用户名: octocat用户 ID: 583231仓库地址: https://api.github.com/users/octocat/repos text

方式1:浏览器直接调用API

不需要写代码,浏览器就可以直接发起HTTP请求查看接口原始返回。

  1. 将接口地址复制粘贴到浏览器地址栏,按下回车

    ···https://api.github.com/users/octocat···

  2. 浏览器直接返回JSON格式原始用户数据。

[!NOTE] JSON是接口通用返回格式,包含用户名、ID、头像、仓库地址等大量字段。

返回片段示例:

json
{
  "login": "octocat",
  "id": 583231,
  "url": "https://api.github.com/users/octocat"
}

方式2:Python 代码调用 API

使用requests库发送 GET 请求,拿到接口返回并解析读取字段。

前置依赖

安装 requests 请求库:

bash
pip install requests

完整代码

python
import requests

# 向GitHub API发送GET请求
response = requests.get("https://api.github.com/users/octocat")
# 将JSON响应转为Python字典
data = response.json()

# 提取字段打印输出
print("用户名:", data["login"])
print("用户ID:", data["id"])
print("仓库地址:", data["repos_url"])

运行命令

bash
python github_api_demo.py

运行结果显示

bash
用户名: octocat
用户ID: 583231
仓库地址: https://api.github.com/users/octocat/repos

[!WARNING] GitHub 公开 API 存在访问频率限制,短时间大量重复请求会被临时限流。