python写webapi_Python系统学习 - WebAPI数据处理

“通过网站的API来编写独立的程序,自动采集数据并对其可视化,使用GitHub API来探索GitHub上星级最高的Python项目“

如上,就是我们今天学习的内容,众所周知,API,即应用程序编程接口,也就是网站、程序开发时预先定义的函数,也就是常说的开源函数,只不过将一些固定的程序或者数据封装在这些函数中,待调用时只需要一个接口引用,方便又简单。

今天我们通过GitHub的API接口,通过get请求使用requests库进行获取,将获取到的json数据,进行处理,将自己需要的数据整理出来,用pygal进行可视化展示成一个条形图先上效果图:

(使用GitHub API来探索GitHub上星级最高的Python项目)上代码:

# coding = utf8

import os

os.path.abspath(".")

import requests

from pygal.style import LightenStyle as LS, LightColorizedStyle as LCS

import pygal

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

hack_news_api_uri = "https://hacker-news.firebaseio.com/v0/item/9884165.json" # Hack News 的API接口

try:

r = requests.get(uri)

request_Dict = r.json()

# print("Connect success, data get normal ……")

# print("Source code: ", r.status_code)

# print("This uri contains keys: %s" %str(request_Dict.keys()))

# 探索有关仓库的信息

# print("There are total : %d repositories in GitHub WebSite." %request_Dict["total_count"])

repositories_Dict = request_Dict["items"]

# print("Repositories returned: %d" %len(repositories_Dict))

# 研究第一个仓库

repositories_Dict_1 = repositories_Dict[0]

# print("The first repositories contains %d Keys!" %len(repositories_Dict_1.keys()))

# print("Let's see these keys:\n ……")

# for key in repositories_Dict_1.keys():

# print(", ")

# 使用Pygal绘制直方图,显示出Github上最受欢迎的一些项目

# names, stars = [], []

# for repositories_Dict_item in repositories_Dict:

# print("")

# print("……Selected information about first repositories:……")

# print("id: ", repositories_Dict_item["id"])

# print("node_id: ", repositories_Dict_item["node_id"])

# print("name: ", repositories_Dict_item["name"])

# print("owner: ", repositories_Dict_item["owner"])

# print("html_url: ", repositories_Dict_item["owner"]["html_url"])

# print("uri: ", repositories_Dict_item["html_url"])

# names.append(repositories_Dict_item["name"])

# stars.append(repositories_Dict_item["stargazers_count"])

# 可视化

# my_style = LS("#336699", base_style = LCS)

# chart = pygal.Bar(style = my_style, x_label_rotation = 35, show_legend = True) # show_legend是否显示图例

# chart.title = "Most - Starred Python project on GitHub"

# chart.x_labels = names

# chart.add("Stargazers", stars) # 给每段数据添加了标签

# chart.render_to_file("./web_Api/chart_Respositories.svg")

# 可视化 & 改进图表样式

my_style = LS("#aabbcc", base_style = LCS)

my_config = pygal.Config() # 创建一个pygal的config对象,将对视图对配置进行管理

my_config.x_label_rotation = 30

my_config.show_legend = True # 是否显示图例

my_config.title_font_size = 24 # 标题字体大小

my_config.label_font_size = 14 # 副标签字体大小 - x 轴对项目名及y轴上对大部分数字

my_config.major_label_font_size = 38 # 主标签字体大小 - y轴上5000的整数倍刻度

my_config.truncate_label = 20 # x轴项目名可显示的字数

my_config.show_y_guides = False # 是否显示y轴刻度线

my_config.width = 1000 # 拉伸图表的宽度以适应显示器

chart = pygal.Bar(my_config, style = my_style)

chart.title = "Most - Starred Python project on GitHub"

# chart.add("Stargazers", stars) # 给每段数据添加了标签

# 可传递字典如stars = {"value" : xx, "label" : "xxx"}, 来为直方图条增加工具提示以特定的描述

names, stars_values_dicts = [], []

for repositories_Dict_item_star in repositories_Dict:

names.append(repositories_Dict_item_star["name"])

star_dict = {

"value" : repositories_Dict_item_star["stargazers_count"],

"label" : str(repositories_Dict_item_star["description"]), # 转换成str类型,因为会存在空值,NoneType,就会报错

"xlink" : repositories_Dict_item_star["html_url"] # 每个条形都关联可跳转链接

}

stars_values_dicts.append(star_dict)

chart.x_labels = names

chart.add("Stargazers", stars_values_dicts)

chart.render_to_file("./web_Api/chart_Respositories.svg")

except requests.exceptions.InvalidSchema:

print("Error, invalid uri")

API的数据处理与Python的爬虫有相似之处也有不同点,爬虫比较常用于直接对网站的源码进行爬取,根据标签来分类处理数据,而API接口获取数据,是源于程序、网站本身提供的接口,是网站默认允许的,因此,我们在进行数据可视化时,对数据的获取,可根据不同情况来使用不同方法去获取哦!

OK,我们下一章开始学习Django啦,这一章Super important,加油!!!

关注 + 收藏 + 点赞哦,谢谢啦~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值