
在浏览器的地址栏中输入:
https://API.github.com/search/repositorIEs?q=language:python&sort=starts
这个API调用github当前托管的python项目
{ "total_count": 3872430,"incomplete_results": true,"items": [ { "ID": 21289110,"node_ID": "MDEwolJlcG9zaXRvcnkyMTI4OTExMA==","name": "awesome-python","full_name": "vinta/awesome-python","private": false,---snip----
"total_count": github上共有的python项目
"incomplete_results": true, 表示请求成功,如果github无法处理该API他返回的值是true
"items"python项目的具体的信息
2 安装requests,requests 可以让python程序轻松向网站请求信息以及检查返回的响应
pip install --user requests
代表仅该用户的安装,安装后仅该用户可用。处于安全考虑,尽量使用该命令进行安装。
3 简单的处理API的响应import requestsurl = ‘https://API.github.com/search/repositorIEs?q=language:python&sort=starts‘r = requests.get(url)# 打印出状态码print(‘status code‘,r.status_code)# status code 200 转态码 200表示成功response_dict = r.Json()# 打印出所有的keysprint(response_dict.keys())# dict_keys([‘total_count‘,‘incomplete_results‘,‘items‘])
4仓库的信息
仓库的信息:
{ "ID": 21289110,"owner": { "login": "vinta","ID": 652070,"node_ID": "MDQ6VXNlcjY1MjA3MA==","avatar_url": "https://avatars2.githubusercontent.com/u/652070?v=4","gravatar_ID": "","url": "https://API.github.com/users/vinta","HTML_url": "https://github.com/vinta","followers_url": "https://API.github.com/users/vinta/followers","following_url": "https://API.github.com/users/vinta/following{/other_user}","gists_url": "https://API.github.com/users/vinta/gists{/gist_ID}","starred_url": "https://API.github.com/users/vinta/starred{/owner}{/repo}","subscriptions_url": "https://API.github.com/users/vinta/subscriptions","organizations_url": "https://API.github.com/users/vinta/orgs","repos_url": "https://API.github.com/users/vinta/repos","events_url": "https://API.github.com/users/vinta/events{/privacy}","received_events_url": "https://API.github.com/users/vinta/received_events","type": "User","site_admin": false },"HTML_url": "https://github.com/vinta/awesome-python","description": "A curated List of awesome Python frameworks,librarIEs,software and resources","fork": false,"url": "https://API.github.com/repos/vinta/awesome-python","forks_url": "https://API.github.com/repos/vinta/awesome-python/forks","keys_url": "https://API.github.com/repos/vinta/awesome-python/keys{/key_ID}","collaborators_url": "https://API.github.com/repos/vinta/awesome-python/collaborators{/collaborator}","teams_url": "https://API.github.com/repos/vinta/awesome-python/teams","hooks_url": "https://API.github.com/repos/vinta/awesome-python/hooks","issue_events_url": "https://API.github.com/repos/vinta/awesome-python/issues/events{/number}","events_url": "https://API.github.com/repos/vinta/awesome-python/events","assignees_url": "https://API.github.com/repos/vinta/awesome-python/assignees{/user}","branches_url": "https://API.github.com/repos/vinta/awesome-python/branches{/branch}","Tags_url": "https://API.github.com/repos/vinta/awesome-python/Tags","blobs_url": "https://API.github.com/repos/vinta/awesome-python/git/blobs{/sha}","git_Tags_url": "https://API.github.com/repos/vinta/awesome-python/git/Tags{/sha}","git_refs_url": "https://API.github.com/repos/vinta/awesome-python/git/refs{/sha}","trees_url": "https://API.github.com/repos/vinta/awesome-python/git/trees{/sha}","statuses_url": "https://API.github.com/repos/vinta/awesome-python/statuses/{sha}","languages_url": "https://API.github.com/repos/vinta/awesome-python/languages","stargazers_url": "https://API.github.com/repos/vinta/awesome-python/stargazers","contributors_url": "https://API.github.com/repos/vinta/awesome-python/contributors","subscribers_url": "https://API.github.com/repos/vinta/awesome-python/subscribers","subscription_url": "https://API.github.com/repos/vinta/awesome-python/subscription","commits_url": "https://API.github.com/repos/vinta/awesome-python/commits{/sha}","git_commits_url": "https://API.github.com/repos/vinta/awesome-python/git/commits{/sha}","comments_url": "https://API.github.com/repos/vinta/awesome-python/comments{/number}","issue_comment_url": "https://API.github.com/repos/vinta/awesome-python/issues/comments{/number}","contents_url": "https://API.github.com/repos/vinta/awesome-python/contents/{+path}","compare_url": "https://API.github.com/repos/vinta/awesome-python/compare/{base}...{head}","merges_url": "https://API.github.com/repos/vinta/awesome-python/merges","archive_url": "https://API.github.com/repos/vinta/awesome-python/{archive_format}{/ref}","downloads_url": "https://API.github.com/repos/vinta/awesome-python/downloads","issues_url": "https://API.github.com/repos/vinta/awesome-python/issues{/number}","pulls_url": "https://API.github.com/repos/vinta/awesome-python/pulls{/number}","milestones_url": "https://API.github.com/repos/vinta/awesome-python/milestones{/number}","notifications_url": "https://API.github.com/repos/vinta/awesome-python/notifications{?since,all,participating}","labels_url": "https://API.github.com/repos/vinta/awesome-python/labels{/name}","releases_url": "https://API.github.com/repos/vinta/awesome-python/releases{/ID}","deployments_url": "https://API.github.com/repos/vinta/awesome-python/deployments","created_at": "2014-06-27T21:00:06Z","updated_at": "2019-07-07T08:12:00Z","pushed_at": "2019-07-05T11:37:09Z","git_url": "git://github.com/vinta/awesome-python.git","ssh_url": "[email protected]:vinta/awesome-python.git","clone_url": "https://github.com/vinta/awesome-python.git","svn_url": "https://github.com/vinta/awesome-python","homepage": "https://awesome-python.com/","size": 4914,"stargazers_count": 69602,"watchers_count": 69602,"language": "Python","has_issues": true,"has_projects": false,"has_downloads": true,"has_wiki": false,"has_pages": true,"forks_count": 13570,"mirror_url": null,"archived": false,"Disabled": false,"open_issues_count": 464,"license": { "key": "other","name": "Other","spdx_ID": "NOASSERTION","url": null,"node_ID": "MDc6TGljZW5zZTA=" },"forks": 13570,"open_issues": 464,"watchers": 69602,"default_branch": "master","score": 1.0 },
简单的仓库的信息做介绍:具体的介绍看文档
响应的API已经按照stargazers_count进行排序了,第一个仓库就是星最高的
name 项目的名称
owner项目的拥有者的信息
stargazers_count 项目获取的星的个数
repos_url 项目的url
created_at 项目创建的时间
updated_at 项目更新的时间
5 打印仓库的信息(项目名称,项目的拥有者,项目的星数,项目的描述)#!/usr/bin/env python# -*- Coding:utf-8 -*-# @Author : linlin# @Software: PyCharmimport requestsurl = ‘https://API.github.com/search/repositorIEs?q=language:python&sort=starts‘r = requests.get(url)# 打印出状态码print(‘status code‘,‘items‘])#共有多少个python的项目print(response_dict[‘total_count‘])# 有关仓库的信息repo_dicts = response_dict[‘items‘]print(‘Repositors returned‘,len(repo_dicts))for repo_dict in repo_dicts: # 项目的名称 print(repo_dict[‘name‘]) # 项目的拥有者 print(repo_dict[‘owner‘][‘login‘]) # 项目的星数 print(repo_dict[‘stargazers_count‘]) # 项目的url print(repo_dict[‘HTML_url‘]) # 项目的描述 print(repo_dict[‘description‘])
6 监视API的速率限制
大多数的API都存在API速率限制,在特定的时间内可以执行的请求数存在限制,看github的限制,请在浏览器上输入https://API.github.com/rate_limit
{ "resources": { "core": { "limit": 60,"remaining": 60,"reset": 1562495790 },"search": { "limit": 10,"remaining": 10,"reset": 1562492250 },"graphql": { "limit": 0,"remaining": 0,"integration_manifest": { "limit": 5000,"remaining": 5000,"reset": 1562495790 } },"rate": { "limit": 60,"reset": 1562495790 }}
search 的解释:
"search": { "limit": 10,每分钟10个请求
"remaining": 10,在当前一分钟还可以请求10次
"reset": 1562492250 },配额将重置unix时间或新世纪时间(1970年1月1日午夜多少秒),当配额用完,您将收到一条简单的响应,有此可知,已达到API的极限,到达极限等待配额重置
配额用完访问https://API.github.com/search/repositorIEs?q=language:python&sort=starts status code 403状态码是403
7 使用pygal可视化仓库使用图形将项目的受欢迎的程度表示出来
import requestsimport pygalfrom pygal.style import lightcolorizedStyle as LCS,lightenStyle as LSurl = ‘https://API.github.com/search/repositorIEs?q=language:python&sort=starts‘r = requests.get(url)# 打印出状态码print(‘status code‘,r.status_code)# status code 200 转态码 200表示成功response_dict = r.Json()# 有关仓库的信息repo_dicts = response_dict[‘items‘]names,starts = [],[]for repo_dict in repo_dicts: names.append(repo_dict[‘name‘]) starts.append(repo_dict[‘stargazers_count‘])# 可视化# 定义图形的格式 333366 蓝色的my_style = LS(‘#333366‘,base_style=LCS)# x_lable_rotation 标签倾斜45度 show_legend=False 隐藏图例chart = pygal.bar(style=my_style,x_lable_rotation=45,show_legend=False)# 图标的标题chart.Title= ‘Most-Stsrred Python Projects on Github‘chart.x_labels = nameschart.add(‘‘,starts)chart.render_to_file(‘python_repos.svg‘)
运行项目会生成python_repos.svg 文件用浏览器打开:
8 改进pygal图标
#!/usr/bin/env python# -*- Coding:utf-8 -*-# @Author : linlin# @Software: PyCharmimport requestsimport pygalfrom pygal.style import lightcolorizedStyle as LCS,base_style=LCS)my_config = pygal.Config()my_config.x_label_rotation = 45my_config.show_legend = False# 图标标题,副标题.,主标签的字体的大小my_config.Title_Font_size = 24my_config.label_Font_size = 14my_config.major_lable_Font_size = 18# 较长的项目名缩短为15个字符,鼠标移动到被截断的项目名,会显示完整的项目名my_config.truncate_label = 15# 隐藏图标的水平线my_config.show_y_guIDes = False# 自动定义高度my_config.wIDth = 1000# x_lable_rotation 标签倾斜45度 show_legend=False 隐藏图例chart = pygal.bar(my_config,style=my_style)# 图标的标题chart.Title= ‘Most-Stsrred Python Projects on Github‘chart.x_labels = nameschart.add(‘‘,starts)chart.render_to_file(‘python_repos.svg‘)
9 添加自定义工具提示
在pygal中,将鼠标指向条形将显示它表示的信息,这通常称为工具提示,创建工具栏,同时显示项目的描述,获取的星
总结以上是内存溢出为你收集整理的python 调用github的api,呈现python的受欢迎的程度全部内容,希望文章能够帮你解决python 调用github的api,呈现python的受欢迎的程度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)