后来证实是输入的数据有问题,需要用str()转化一下,修改后的代码如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'处理API响应'
__author__ = 'Luna'
import requests
import sys
import io
import pygal
from pygal.style import LightenStyle as LS,LightColorizedStyle as LCS
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')
url='https://api.github.com/search/repositories'
url+='?q=language:c&sort=stars'
r=requests.get(url)
print('响应结果: ',r.status_code)
reaponses_dict=r.json()
print(reaponses_dict.keys())
print('total count: ',str(reaponses_dict['total_count']))
repo_dicts=reaponses_dict['items']
print('\n仓库返回',len(repo_dicts))
names=[]
plot_dicts=[]
print('每个仓库的相关信息')
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
plot_dict={
'value':repo_dict['stargazers_count'],
#此处‘label’的内容修改
'label':str(repo_dict['description']),
'xlink':repo_dict['html_url'],
}
plot_dicts.append(plot_dict)
print(plot_dicts)
python_style=LS('#336699',base_style=LCS)
chart=pygal.Bar(style=python_style,x_label_rotation=45,show_legend=False)
chart.title='最受欢迎的python项目'
chart.x_labels=names
file_path='C:\\Users\\luna\\Desktop\\documents\\data_science\\data_science\\python\\Python_first\\project_data\\下载数据\\使用API\\python_link.svg'
chart.add('',plot_dicts)
chart.render_to_file(file_path)
网友评论