通过API读取GitHub中的Python仓库并将前30可视化

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)

#查看响应状态看是否响应成功

print('Status code:',r.status_code)

#将API的响应数据存储在变量中

response_dict = r.json()

#处理结果

print(response_dict.keys())

print('Total repositories:',response_dict['total_count'])

#探索有关仓库的信息

repo_dicts = response_dict['items']

print('Repositories returned:',len(repo_dicts))

'''#研究第一个仓库

repo_dicts_1 = repo_dicts[0]

print('\nKeys:',len(repo_dicts_1))

#按字母升序

for key in sorted(repo_dicts_1.keys()):

print(key)'''

names, plot_dicts,stars =[],[],[]

#print('\nSelected information about each repository:')

for repo_dict in repo_dicts:

'''print('\nName: ',repo_dict['name'])

print('Owner: ',repo_dict['owner']['login'])

print('Stars: ',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'])'''

names.append(repo_dict['name'])

stars.append(repo_dict['stargazers_count'])

if repo_dict['description'] != None:

plot_dict = {'value':repo_dict['stargazers_count'],

'label':repo_dict['description'],

}

else:

plot_dict = {'value':repo_dict['stargazers_count'],

'label':'Nothing!',

}

plot_dicts.append(plot_dict)

#可视化

my_style = LS('#333366',base_style=LCS)

my_config = pygal.Config()

my_config.x_label_rotation = 45

my_config.show_legend = False

my_config.title_font_size = 24

my_config.label_font_size = 14

my_config.major_label_font_size = 18

#将过长的横坐标缩到15个字符

my_config.truncate_label = 15

#隐藏水平线

my_config.show_y_guides = False

my_config.width = 1000

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

chart.title = 'Most_Starred Python Projects on GitHub'

chart.x_labels = names

#plot_dicts = plot_dicts[:20]

chart.add('',plot_dicts)

#print(plot_dicts)

chart.render_to_file('python_repos.svg')



通过API读取GitHub中的Python仓库并将前30可视化_第1张图片


其中有一个异常的项目,字典的键值为空 因此无法画图,主要是无法为空的字符进行编码,于是通过判断将该键值对选出来并将descripyion改为Nothing!  想对这个特殊项目着色 ,但是不会,以后学习了再来修改吧!

你可能感兴趣的:(通过API读取GitHub中的Python仓库并将前30可视化)