python学习实践,根据github的API获取某个语言的项目列表生成图表
通过python的requests
模块,对github的https://api.github.com/search/repositories?q=language:javascript&sort=starts
接口进行请求,来获取我们需要搜索的语言数据。如果需要获取其他语言的数据,只要修改API的javascript为想要查看的语言即可。
具体代码:
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
url = 'https://api.github.com/search/repositories?q=language:javascript&sort=starts'
r = 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))
# names, stars = [], []
names, plot_dicts = [], []
print('\n Selected information about first respository:')
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
# stars.append(repo_dict['stargazers_count'])
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': repo_dict['description'],
'xlink': repo_dict['html_url']
}
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_y_guides = False
chart = pygal.Bar(my_config, show_legend=False)
chart.title = 'Most-Starred Python Projects onn GitHub'
chart.x_labels = names
chart.add('stars', plot_dicts)
chart.render_to_file('python_respos.svg')
通过requests.get(url)
获取我们需要的数据,通过pygal.Bar创建一个图标,先列表一下my_config都代表什么意思。
- pygal.Config(): 创建一个Pygal类Connfig的实例
- x_label_rotation: 让标签绕x轴旋转度数
- show_y_guides: 隐藏x轴上的水平线
方法add()接收一个字符串和一个列表,render_to_file()这里我让其生成一个svg文件,列表的效果图如下: