代碼:
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(r.status_code)
#響應存儲在變量中
response_dic = r.json()
#print(response_dic.keys())
print("total:"+str(response_dic["total_count"]))
#respon_dic :所有倉庫,是一個列表,有很多字典
respon_dics = response_dic["items"]
print("dics:"+str(len(respon_dics)))
"""找出第一個字典
respon_dic = respon_dics[0]
print("\nkeys:"+str(len(respon_dic)))
for key in sorted(respon_dic.keys()):
print(key)"""
names,plot_dicts = [],[]
for respon_dic in respon_dics:
names.append(respon_dic['name'])
plot_dict = {
'value':respon_dic['stargazers_count'],
'label':respon_dic['description']
}
plot_dicts.append(plot_dict)
#plot_dicts.append(respon_dic['stargazers_count'])
#可視化
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
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 stared python projects on github"
chart.x_labels= names
chart.add('',plot_dicts)
chart.render_to_file("python_respon.svg")
報錯:
Traceback (most recent call last):
File "F:\workspace\python\api\python_request.py", line 62, in <module>
chart.render_to_file("python_respon.svg")
File "C:\Users\zawdcxs\AppData\Local\Programs\Python\Python36\lib\site-packages\pygal\graph\public.py", line 114, in render_to_file
f.write(self.render(is_unicode=True, **kwargs))
File "C:\Users\zawdcxs\AppData\Local\Programs\Python\Python36\lib\site-packages\pygal\graph\public.py", line 52, in render
self.setup(**kwargs)
File "C:\Users\zawdcxs\AppData\Local\Programs\Python\Python36\lib\site-packages\pygal\graph\base.py", line 217, in setup
self._draw()
File "C:\Users\zawdcxs\AppData\Local\Programs\Python\Python36\lib\site-packages\pygal\graph\graph.py", line 933, in _draw
self._plot()
File "C:\Users\zawdcxs\AppData\Local\Programs\Python\Python36\lib\site-packages\pygal\graph\bar.py", line 146, in _plot
self.bar(serie)
File "C:\Users\zawdcxs\AppData\Local\Programs\Python\Python36\lib\site-packages\pygal\graph\bar.py", line 116, in bar
metadata)
File "C:\Users\zawdcxs\AppData\Local\Programs\Python\Python36\lib\site-packages\pygal\util.py", line 233, in decorate
metadata['label'])
File "C:\Users\zawdcxs\AppData\Local\Programs\Python\Python36\lib\site-packages\pygal\_compat.py", line 61, in to_unicode
return string.decode('utf-8')
AttributeError: 'NoneType' object has no attribute 'decode'
仔細校對和書上的對比過都打的一樣了,還是報錯。。
最後看到關鍵3行:
metadata['label'])
return string.decode('utf-8')
AttributeError: 'NoneType' object has no attribute 'decode'
”空類型“對象沒有屬性“decode”
應該是label屬性 期望是個字符串?
for respon_dic in respon_dics:
names.append(respon_dic['name'])
plot_dict = {
'value':respon_dic['stargazers_count'],
'label':respon_dic['description']
}
plot_dicts.append(plot_dict)
#plot_dicts.append(respon_dic['stargazers_count'])
裏面'label':respon_dic['description']改爲'label':str(respon_dic['description'])
搞定。
效果: