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文件,列表的效果圖如下:
1534054272085.jpg