用python爬github上星级排名前列的项目报错AttributeError: 'NoneType' object has no attribute 'decode'
2017-08-29 21:48
1171 查看
代码:
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'])
搞定。
效果:
相关文章推荐
- Python脚本报错AttributeError: ‘module’ object has no attribute ’urllibopen'(Python 2.7.11 )
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- python图像处理问题AttributeError: 'NoneType' object has no attribute 'shape'
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python之报错AttributeError:'CocaCola' object has no attribute 'local_logo'
- pygal制图“AttributeError: 'NoneType' object has no attribute 'decode'”
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- python __def__ Exception AttributeError: "'NoneType' object has no attribute
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python报错之:AttributeError: 'NoneType' object has no attribute 'seq'
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python: Attribute Error - 'NoneType' object has no attribute 'something'
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’
- python3.6报错:AttributeError: 'str' object has no attribute 'decode'
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法