忻州建设厅官方网站,dedecms网站迁移,wordpress上传大文件,网站建设相关工作文章目录 Web APIGit 和 GitHub使用 API 调用请求数据安装 requests处理响应 API处理响应字典监视API的速率限制使用 Pygal 可视化仓库改进Pygal图表添加自定义工具提示 本篇文章#xff1a;我们叙述如何编写一个独立的程序#xff0c;并对其获取的数据进行可视化。这个程序将… 文章目录 Web APIGit 和 GitHub使用 API 调用请求数据安装 requests处理响应 API处理响应字典监视API的速率限制使用 Pygal 可视化仓库改进Pygal图表添加自定义工具提示 本篇文章我们叙述如何编写一个独立的程序并对其获取的数据进行可视化。这个程序将使用 Web应用编程接口API自动请求网站的特点信息而不是整个网页再对这些信息进行可视化。 Web API Web API 是网站的一部分用于与使用非常具体的URL请求特定信息的程序交互。这种请求称为APi调用。请求的数据将以易于处理的格式如JSON或CSV返货。依赖于外部数据源的大多数应用程序都依赖于API调用如集成社交媒体网站的应用程序。 Git 和 GitHub
Git 分布式版本控制系统… … GitHub 程序员协作开发项目网站… … 使用 API 调用请求数据
GitHub 的API 让我们能够通过API调用来请求各种信息。
https://api.github.com/search/repositories?qlanguage:pythonsortstars这个调用返回GItHub当前托管了多少个Python项目还有有关最受欢迎的Python仓库的信息。
第一部分(https://api.github.com/) 将请求发送到GitHub网站中响应API调用的部分
第二部分(searfch/respositories) 让API搜索GItHub上的所有仓库
responsitories 后面的问号指出我们要传递一个参数。q表示查询而等号让我们能够开始指定查询 (q) 。通过使用 language : python 我们指出只想获取主要语言为 python的仓库的信息。
最后一部分(sortstars) 执行项目按其获得的星级进行排序。
安装 requests requests 包让python程序能够轻松地向网站请求信息以及检查返回的响应。 安装命令
pip install --user requests处理响应 API
我们来尝试编写一个程序它执行 API 调用并处理结果找出 GItHub 上星级最高的Python项目
# 导入requests 包
import requestsurl https://api.github.com/search/repositories?qlanguage:pythonsortstarsr requests.get(url)
print(Starts code:,r.status_code)response_dict r.json()
print(response_dict.keys())代码解析 import requests 用于导入 requests 模块 然后存储API 将要调用的URL然后使用 requests 来执行调用。 我们调用 get() 并将 url 传递给它再将响应对象存储在变量当中。 响应对象包含一个名为 status_code 的属性它让我们知道请求是否成功了(状态码为200表示成功) API 返回的信息格式为JSON因为们还使用json() 将这些信息转换为一个 python 字典。 执行结果
处理响应字典
现在将存储在字典当中的api调用返回信息进行处理
import requestsURL https://api.github.com/search/repositories?qlanguage:pythonsortstarsr requests.get(URL)
print(Status code:, r.status_code)response_dict r.json()
print(Total repositories:,response_dict[total_count])#探索有关仓库的信息
repo_dicts response_dict[items]
print(Repositories returned:, len(repo_dicts))#研究第一个仓库
repo_dict repo_dicts[0]for key in sorted(repo_dict.keys()):print(key)
代码解析 total_count 指出了 github 总共包含了多少个python仓库 与’items’相关的是一个列表其中包含很多字典而每个字典都包含有关一个 Python仓库的信息。打印 repo_dicts 的长度用于知道我们获得了多少个仓库的信息。 repo_dicts 是为了更深入地了解返回的有关每个仓库的信息将repo_dictss 中的第一个字典提取并存储在变量 repo_dicts 中。打印该字典包含的键数查看其中包含多少信息。 repo_dict 中包含的键有
提取其中一些键所对应的值
print(\nSelected information about first repository:)
print(Name:,repo_dict[name])
print(Owner:,repo_dict[owner][login])
print(Stats:,repo_dict[stargazers_count])
print(Repository,repo_dict[html_url])
print(Created:,repo_dict[created_at])
print(Updated:,repo_dict[updated_at])
print(Description:,repo_dict[description])运行结果 我们打印了表示第一仓库的字典中与很多键相关联的值我们打印了项目的名称而项目的名称是用一个字典来表示的所以我们使用关键字 Owner来访问表示所有者的字典再使用 key来获取所有者的登录名。 该项目的名称为public-api所有用户Owner: public-apis。其中有 278863个用户给这个项目加星。 Repository 是该项目的 URL而Created是其创建时间Updated是其最近更新时间。 如果你想提取更多的其他项目相关信息可以在提取之前加一层循环。
print(\nSelected information about first repository:)for repo_dict in repo_dicts:print(Name:,repo_dict[name])print(Owner:,repo_dict[owner][login])print(Stats:,repo_dict[stargazers_count])print(Repository,repo_dict[html_url])print(Created:,repo_dict[created_at])print(Updated:,repo_dict[updated_at])print(Description:,repo_dict[description])监视API的速率限制 大多数 API 都存在速率限制即你在特定时间内可执行的请求数存在限制。要获悉GitHub的限制可以在浏览器中录入 https://api.github.com/rate_limit 在这条信息中 : search :{ “limit” :10 } 可得知极限为每分钟10个请求而在当前的一分钟内我们还可以发起10个请求即 “remaining”:8 使用 Pygal 可视化仓库
为了更方便的查阅信息我们将其进行视图可视化
希望将信息以视图方式展示需要先导入 pygal包。
当然如果你用 pycharm 编辑代码别忘了在集成工具中进行安装
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS#用于存储包含图表信息的两个空列表
names,stars [],[]for repo_dict in repo_dicts:names.append(repo_dict[name])stars.append(repo_dict[stargazers_count])my_style LS(#333366,base_style LCS)chart pygal.Bar(style my_style,x_label_rotation 45,show_legend False)chart.title Most-Starred Python Projects on GitHubchart.x_labels nameschart.add(,stars)chart.render_to_file(python_repos.svg)这段代码也是基于之前的demo继续加的内容这里不在进行过多赘述如果有感兴趣的小伙伴可以自行深究一下但内容其实还是简单的。 这里在看一下运行效果
图表可视化成功之后我们继续迭代。
改进Pygal图表
下面我们对图表的样式进行改造。
我们需要进行多方面的定制因此先来稍微调整代码的结构创建一个配置对象在其中包含要传递给Bar() 的所有定制
names,stars [],[]for repo_dict in repo_dicts:names.append(repo_dict[name])stars.append(repo_dict[stargazers_count])my_style LS(#333366,base_style LCS)#创建配置对象my_config pygal.Config()my_config.x_label_rotation 45my_config.show_legend Falsemy_config.title_font_size 24my_config.label_font_size 14my_config.major_label_font_size 18my_config.truncate_label 15my_config.show_y_guides Falsemy_config.width 1000chart pygal.Bar(my_config,style my_style)chart.title Most-Starred Python Projects on GitHubchart.x_labels nameschart.add(,stars)chart.render_to_file(python_repos.svg)添加自定义工具提示 在pygal中将鼠标指向条形将显示它表示的信息这通常称为工具提示。 chart pygal.Bar(my_config,style my_style,x_label_rotation 45,show_legendFalse)chart.title Python projectschart.x_labels [httpie,djange,flask]plot_dicts [{value: 16101,label:Description of httpie.},{value: 15028,label:Description of django.},{value: 14798,label:Description of flask.},]chart.add(,plot_dicts)chart.render_to_file(bar_descriptions.svg)运行效果