import requests
import pygal
from pygal.style import LightColorizedStyle as LCS # 类
from pygal.style import LightenStyle as LS # 类
from requests.packages import urllib3
urllib3.disable_warnings() # 关闭warning
# 执行API调用
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url, verify=False) # 返回JSON格式信息
print("Status code:", r.status_code) # 属性表示是否请求成功,200表示成功。200
# 存储API响应
response_dict = r.json() # 把JSON转换为一个Python字典
print("Total repositories:", response_dict['total_count']) # 5577125
# 仓库
repo_dicts = response_dict['items'] # 每个元素都是一个字典
# 存储名字,获得的星星
names, stars, plot_dicts = [], [], []
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 # x轴标签旋转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
my_config.truncate_label = 15 # 名称限制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
# chart.add('', plot_dicts)
chart.add('', stars)
chart.render_to_file('python_repos.svg')
使用chart.add('', plot_dicts)作图会报错,但是用chart.add('', stars)不会,是因为这个列表哪里搞错了吗