使用api.github.com作为接口,快速查看Github星星数等信息
api.github.com是Github的api接口
下载后会有一个Github信息快速查看器.exe的文件,这就是软件,双击打开后,输入仓库的拥有者,以及仓库名,即可查看相关信息
Github-Quicklook官方网站:https://Wdataorg/Github-Quicklook
我们将使用Python代码实现
以下属于进阶内容,讲解的是本项目的实现方法
from requests import get # 导入请求函数
response = get('https://api.github.com/repos/{UserName}/{RepoName}') # 向API接口请求
print(response.content) 以Wdataorg/Github-Quicklook为例,输出是这样
PS D:\Github-Quicklook> & C:/Users/S.X.Y/AppData/Local/Programs/Python/Python38-32/python.exe d:/Github-Quicklook/test/test.py
b'{"id":536071343,"node_id":"R_kgDOH_PMrw","name":"Github-Quicklook","full_name":"Wdataorg/Github-Quicklook","private":false,"owner":{"login":"Wdataorg","id":107593423,"node_id":"O_kgDOBmm-zw","avatar_url":"https://avatars.githubusercontent.com/u/107593423?v=4","gravatar_id":"","url":"https://api.github.com/users/Wdataorg","html_url":"https://github.com/Wdataorg","followers_url":"https://api.github.com/users/
......(太多了)
他的格式是json, 没法直接使用,还得转换成dict
以下是转化的代码
json_data = response.content
import json # 导入Json类
dict_data = json.loads(json_data) # 转化成Dict类型
print(type(dict_data)) # 打印类型输出是<class 'dict'>,得到了Dict类型的数据,我们就可以直接分析了!
但我们要了解网页返回的json类型中的一些数据
eg. 类型
"size":11671 显示仓库大小
"forks_count":0 显示仓库fork数量
"stargazers_count": 1 显示仓库Star数量
"watchers_count": 1 显示仓库Watch数量
了解了这些信息,我们就可以补全查询仓库信息的代码
print('星星数量\tWatch数量\tFork数量\t仓库大小')
print('{}\t{}\t{}\t{}'.format(dict_data['stargazers_count'], dict_data["watchers_count"], dict_data["forks_count"],dict_data["size"])) # 打印信息完整代码如下:
from requests import get # 导入请求函数
response = get('https://api.github.com/repos/Wdataorg/Github-Quicklook') # 向API接口请求
json_data = response.content
import json
dict_data = json.loads(json_data)
print('星星数量\tWatch数量\tFork数量\t仓库大小')
print('{}\t{}\t{}\t{}'.format(dict_data['stargazers_count'], dict_data["watchers_count"], dict_data["forks_count"],dict_data["size"])) # 打印信息输出如下:
PS D:\Github-Quicklook> & C:/Users/S.X.Y/AppData/Local/Programs/Python/Python38-32/python.exe d:/Github-Quicklook/test/test.py
星星数量 Watch数量 Fork数量 仓库大小
1 1 0 11671