Python3使用Web API处理数据

目录

1、安装requests包

2、Python3使用Web API处理数据


1、安装requests包

2、Python3使用Web API处理数据

import requests
import pygal
from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS
#执行API调用并存储响应
url='https://api.github.com/search/repositories?q=language:python&sort=stars'#存储API调用的URL
r=requests.get(url) #获得URL对象
print("Status code:",r.status_code) #判断请求是否成功(状态码200时表示请求成功)
response_dict=r.json() #API返回json格式的信息(将响应存储到变量中)
print("Total repositories:",response_dict['total_count'])
#探索有关仓库的信息
repo_dicts=response_dict['items']
'''
print("Repositories returned:",len(repo_dicts))  #获得了多少个仓库
#研究第一个仓库
repo_dict=repo_dicts[0]
print('\nKeys:',len(repo_dict))
for key in sorted(repo_dict.keys()):
    print(key)
'''
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_lable_rotation=45,show_legend=False)#创建一条简单的条形图
chart._title='Most-Starred Python Projects on GitHub'
chart.x_labels=names
chart.add('',stars)
chart.render_to_file('python_repos.svg')

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Asia-Lee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值