Web API

web api是网站的一部分,用于与使用非常具体的URL请求特定信息的程序交互,这种请求被称为API调用。请求的数据将以易于处理的格式(如JSON或CSV)返回;依赖于外部数据源的大多数应用程序都依赖于API调用,如集成社交媒体网站的应用程序。

Github的API

https://api.github.com/search/repositories?q=language:python&sort=stars

第一部分(https://api.github.com/)将请求发送到Github网站响应API调用的部分;search/repositories让API搜索github上所有的仓库,问号指出要传递一个实参,q=开始指定查询,获取语言为Python的仓库的信息,最后一部分指定将项目按其获得的星级排序。如果incomplete_results的值为false,证明请求成功,true代表GitHub无法全面处理该API。

API大多存在速率限制,即在特定时间内执行的请求数存在限制,在浏览器中输入https://api.github.com/rate_limit,可获得GitHub的限制;search表示每分钟限制10个请求,reset值指的是配额将重置的Unix时间或新纪元时间(1970年1月1日午夜后多少秒),注册获得API秘钥之后,配额将高很多。

{
"resources": {
"core": {
"limit": 60,
"remaining": 60,
"reset": 1542466950
},
"search": {
"limit": 10,
"remaining": 10,
"reset": 1542463410
},
"graphql": {
"limit": 0,
"remaining": 0,
"reset": 1542466950
}
},
"rate": {
"limit": 60,
"remaining": 60,
"reset": 1542466950
}
}

--将项目名存储在一个列表中,星级、描述、链接放在一个字典里,使用pygal绘制直方图,每个条形柱会存储一个连接,点击可直接转到所在网页。
hist = pygal.Bar()
hist.add('',plot_dicts)
import requests
import pygal
from pygal.style import LightenStyle as LS,LightColorizedStyle as LCS
#执行API调用,并存储响应,以星级排序
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
#状态码200表示请求成功
print('Status code:',r.status_code)
#将API响应返回的JSON格式的信息转换为一个Python字典
respose_dict = r.json()
print('Total repositories:',respose_dict['total_count'])#一共多少个仓库
#items是一个包含很多字典的列表,每一个字典都包含一个仓库的信息
repo_dicts = respose_dict['items']
#研究第一个仓库
repo_dict = repo_dicts[0]
print('Keys:',len(repo_dict))
print('\nName:',repo_dict['name'])#项目名称
print('Owner:',repo_dict['owner']['login'])#项目所有者登录名
print('Stars:',repo_dict['stargazers_count'])#多少个星的评级
print('Repository:',repo_dict['html_url'])#GitHub仓库的URL
print('Created:',repo_dict['created_at'])#创建时间
print('Updated:',repo_dict['updated_at'])#最后一次更新时间
print('Description:',repo_dict['description'])#打印仓库描述
#2015年GitHub上星级最高的项目,收藏人数16000
for rp_dict in repo_dicts:
if rp_dict['name'] == 'httpie':
#rp_index = repo_dicts.index(rp_dict)
print('\nOwner:', rp_dict['owner']['login'])
print('Stars:', rp_dict['stargazers_count'])
print('Created:', rp_dict['created_at'])
print('Updated:', rp_dict['updated_at'])
#将项目名存储在一个列表中,星级、描述、链接放在一个字典里
names,plot_dicts = [],[]
for r_dict in repo_dicts:
names.append(r_dict['name'])
if r_dict['description']:
plot_dict = {
'value':r_dict['stargazers_count'],
'label':r_dict['description'],
'xlink':r_dict['html_url']
}
plot_dicts.append(plot_dict)
else:
plot_dict = {
'value': r_dict['stargazers_count'],
'label': 'None',
'xlink': r_dict['html_url']
}
plot_dicts.append(plot_dict)
#将GitHub上Python项目的星级和名称可视化
my_style = LS('#119911',base_style=LCS)#指定颜色和样式
#绘制直方图,创建一个Config实例,存储所有的配置
my_config = pygal.Config()
my_config.x_label_rotation = (-45) #x轴标签旋转-45度
my_config.show_legend = False #隐藏图例
my_config.title_font_size = 25 #标题大小
'''
主标签是Y轴上为5000整数倍的刻度,副标签是X轴上的项目
名称和Y轴上的大部分数字,但是显示效果并非如此
'''
my_config.label_font_size = 18 #副标签大小
my_config.major_label_font_size = 30 #主标签大小
#将较长的标签缩短为15个字符,鼠标指向时显示完整名称
my_config.truncate_label = 15
my_config.show_y_guides = False #隐藏图表中的水平线,包括x轴
my_config.width = 1000 #设置图表宽度
hist = pygal.Bar(my_config,style=my_style)
hist.title = 'Most-Popular Python Projects on GitHub'
hist.x_labels = names
hist.add('',plot_dicts)#图例名称为空
hist.render_to_file(r'images\names_stars.svg')
# 直接渲染到浏览器,需要安装lxml
#hist.render_in_browser()
 

Github API的更多相关文章

  1. 记一次通过c#运用GraphQL调用Github api

    阅读目录 GraphQL是什么 .net下如何运用GraphQL 运用GraphQL调用Github api 结语 一.Graphql是什么 最近在折腾使用Github api做个微信小程序练练手,本 ...

  2. 关于Homebrew出现GitHub API rate limit错误的解决方法

    参考博文: http://havee.me/mac/2013-12/how-to-install-and-use-homebrew.html Error: GitHub API rate limit ...

  3. 使用 GitHub API 进行数据分析 (Node.js)

    使用 GitHub API 进行数据分析 (Node.js) Node.js 的访问 GitHub 的 API 库,通过 npm 或者 yarn 安装: yarn add github-api 官方示 ...

  4. Github api【Restful接口规范】

    Overview This describes the resources that make up the official GitHub REST API v3. If you have any ...

  5. 从0开始学爬虫10之urllib和requests库与github/api的交互

    urllib库的使用 # coding=utf-8 import urllib2 import urllib # htpbin模拟的环境 URL_IP="http://10.11.0.215 ...

  6. 获取使用GitHub api和Jira api Authentication的方法

    近段时间在搭建我司的用例管理平台,有如下需求: 1.需要根据项目--版本--轮次的形式来管理项目用例,用例统一保存在git工程. 2.执行用例时,如果用例执行失败,可以通过平台在Jira上提bug. ...

  7. 使用GitHub API上传文件及GitHub做图床

    本文介绍GitHub API基础及上传文件到仓库API,并应用API将GitHub作为图床 GitHub API官方页面 GitHub API版本 当前版本为v3,官方推荐在请求头中显示添加版本标识. ...

  8. 博客中gitalk最新评论的获取 github api使用

    博客中,对于网友的评论以及每篇文章的评论数还是很重要的.但是基于静态的页面想要存储动态的评论数据是比较难的,一般博客主题中都内置了评论插件,但是博客主题中对于最新评论的支持显示还是很少的,至少目前我是 ...

  9. GitHub developer API 学习

    官网地址:https://developer.github.com/v3/ 目录 当前版本 schema parameters root endpoint client errors http red ...

随机推荐

  1. SQL Server性能优化(10)非聚集索引的存储结构

    一,新建测试表 CREATE TABLE [dbo].[Users]( ,) NOT NULL, ) NOT NULL, [CreatTime] [datetime] NOT NULL ) ON [P ...

  2. MySQL数据库设计规范

    1. 规范背景与目的 MySQL数据库与 Oracle. SQL Server 等数据库相比,有其内核上的优势与劣势.我们在使用MySQL数据库的时候需要遵循一定规范,扬长避短.本规范旨在帮助或指导R ...

  3. 【jQuery源码】整体架构

    jQuery源码可以精简为以下内容: 方框上面的代码根据Jq注释我们可以知道是对AMD规范的支持. jQuery整体上被包裹在一个匿名函数中,这个匿名函数再作为另一个匿名函数的参数被传入,形参fact ...

  4. JavaWeb学习 (十九)————JavaBean

    一.什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参的构造函数 属性必须私有化. 私有化的属性必须通过public类型的方法 ...

  5. [转]angular2中ng alerts的使用教程

    本文转自:https://blog.csdn.net/m0_37981481/article/details/79281879 由于想要一个好看的alert,于是去npm上搜了一下,手动捂脸,npm上 ...

  6. HTML DOM querySelector() 方法

     Document 对象 实例 获取文档中 id="demo" 的元素: document.querySelector("#demo"); <!DOCTY ...

  7. 【手记】解决“未能创建 SSL/TLS 安全通道”异常

    之前写了一个桌面程序,程序会间歇性访问某个https接口,一直用的好好的,今天突然报错了,异常就发生在访问接口的地方,曰“请求被中止,未能创建 SSL/TLS 安全通道.”,另外有台电脑也有跑该程序, ...

  8. Spring中四种实例化bean的方式

    本文主要介绍四种实例化bean的方式(注入方式) 或者叫依赖对象实例化的四种方式.上面的程序,创建bean 对象,用的是什么方法 ,用的是构造函数的方式 (Spring 可以在构造函数私有化的情况下把 ...

  9. Matlab illustrate stiffness

    % matlab script to illustrate stiffness % using simple flame propagation model close all clear all % ...

  10. springboot整合freemarker----一点小小的错误

    最近小弟正在学习springboot,没办法,现在微服务太火了.小弟也要顺应时代的潮流啊 :( 好了,废话不多说了!!!! 首先在springboot的pom.xml添加freemarker的依赖 & ...