Python3——使用Web API处理数据

安装requests包

 

python_repo.py

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')

 

 

 

 

 

全部评论

相关推荐

07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
小叮当411:应该是1-3个月吧
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务