实现
由于 GitHub 提供无需令牌支持的 API,我们可以直接访问以下网址来获取一个仓库的信息:
https://api.github.com/repos/{owner}/{repo}
这里以我的仓库为例,使用浏览器访问,可以看到返回的是一个仓库信息的元数据:
{
"id": 802463574,
"node_id": "R_kgDOL9SfVg",
"name": "PyQt-SiliconUI",
"full_name": "ChinaIceF/PyQt-SiliconUI",
"private": false,
"owner": {
"login": "ChinaIceF",
"id": 77963357,
"node_id": "MDQ6VXNlcjc3OTYzMzU3",
"avatar_url": "https://avatars.githubusercontent.com/u/77963357?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/ChinaIceF",
"html_url": "https://github.com/ChinaIceF",
"followers_url": "https://api.github.com/users/ChinaIceF/followers",
"following_url": "https://api.github.com/users/ChinaIceF/following{/other_user}",
"gists_url": "https://api.github.com/users/ChinaIceF/gists{/gist_id}",
"starred_url": "https://api.github.com/users/ChinaIceF/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/ChinaIceF/subscriptions",
"organizations_url": "https://api.github.com/users/ChinaIceF/orgs",
"repos_url": "https://api.github.com/users/ChinaIceF/repos",
"events_url": "https://api.github.com/users/ChinaIceF/events{/privacy}",
"received_events_url": "https://api.github.com/users/ChinaIceF/received_events",
"type": "User",
"site_admin": false
},
"html_url": "https://github.com/ChinaIceF/PyQt-SiliconUI",
"description": "A powerful and artistic UI library based on PyQt5,基于 PyQt5 的UI框架,灵动、优雅而轻便",
"fork": false,
"url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI",
"forks_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/forks",
"keys_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/teams",
"hooks_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/hooks",
"issue_events_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/issues/events{/number}",
"events_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/events",
"assignees_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/assignees{/user}",
"branches_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/branches{/branch}",
"tags_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/tags",
"blobs_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/statuses/{sha}",
"languages_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/languages",
"stargazers_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/stargazers",
"contributors_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/contributors",
"subscribers_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/subscribers",
"subscription_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/subscription",
"commits_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/contents/{+path}",
"compare_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/merges",
"archive_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/downloads",
"issues_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/issues{/number}",
"pulls_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/pulls{/number}",
"milestones_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/milestones{/number}",
"notifications_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/labels{/name}",
"releases_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/releases{/id}",
"deployments_url": "https://api.github.com/repos/ChinaIceF/PyQt-SiliconUI/deployments",
"created_at": "2024-05-18T11:15:20Z",
"updated_at": "2024-10-06T15:18:35Z",
"pushed_at": "2024-09-29T06:35:28Z",
"git_url": "git://github.com/ChinaIceF/PyQt-SiliconUI.git",
"ssh_url": "[email protected]:ChinaIceF/PyQt-SiliconUI.git",
"clone_url": "https://github.com/ChinaIceF/PyQt-SiliconUI.git",
"svn_url": "https://github.com/ChinaIceF/PyQt-SiliconUI",
"homepage": "",
"size": 12309,
"stargazers_count": 460,
"watchers_count": 460,
"language": "Python",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"has_discussions": true,
"forks_count": 44,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 11,
"license": {
"key": "gpl-3.0",
"name": "GNU General Public License v3.0",
"spdx_id": "GPL-3.0",
"url": "https://api.github.com/licenses/gpl-3.0",
"node_id": "MDc6TGljZW5zZTk="
},
"allow_forking": true,
"is_template": false,
"web_commit_signoff_required": false,
"topics": [
"gui",
"pyqt",
"pyqt5",
"pyqt6",
"pyside",
"python",
"qt5",
"qt6",
"ui",
"ui-components",
"ui-design"
],
"visibility": "public",
"forks": 44,
"open_issues": 11,
"watchers": 460,
"default_branch": "main",
"temp_clone_token": null,
"network_count": 44,
"subscribers_count": 5
}
搭配 Python 的 requests 库,我们可以用如下方式请求并解析我们所需的数据:
response = requests.get(url)
repo_info = response.json()
stars_count = repo_info['stargazers_count'] # stars 数量
类似地,你可以将stargazers_count
替换为你想获得的值的键。
完整示例程序
import requests
owner = 'chinaicef'
repo = 'pyqt-siliconui'
url = f'https://api.github.com/repos/{owner}/{repo}'
response = requests.get(url)
# 通过状态码检查请求是否成功
if response.status_code == 200:
repo_info = response.json()
stars_count = repo_info['stargazers_count']
print(f'{repo} has {stars_count} stars.')
else:
print('Error fetching repository data.')