用python爬github上星级排名前列的项目报错AttributeError: ‘NoneType’ object has no attribute ‘decode’


代
码:
  1. import requests
  2. import pygal
  3. from pygal.style import LightenStyle as ls,LightColorizedStyle as lcs
  4. #执行api调用并存储相应
  5. url = ‘https://api.github.com/search/repositories?q=language:python&sort=stars’
  6. r = requests.get(url)
  7. print(r.status_code)
  8. #响应存储在变量中
  9. response_dic = r.json()
  10. #print(response_dic.keys())
  11. print(“total:”+str(response_dic[“total_count”]))
  12. #respon_dic :所有仓库,是一个列表,有很多字典
  13. respon_dics = response_dic[“items”]
  14. print(“dics:”+str(len(respon_dics)))
  15. “””找出第一个字典
  16. respon_dic = respon_dics[0]
  17. print(“\nkeys:”+str(len(respon_dic)))
  18. for key in sorted(respon_dic.keys()):
  19. print(key)”””
  20. names,plot_dicts = [],[]
  21. for respon_dic in respon_dics:
  22. names.append(respon_dic[‘name’])
  23. plot_dict = {
  24. ‘value’:respon_dic[‘stargazers_count’],
  25. ‘label’:respon_dic[‘description’]
  26. }
  27. plot_dicts.append(plot_dict)
  28. #plot_dicts.append(respon_dic['stargazers_count'])
  29. #可视化
  30. my_style= ls(‘#333366′,base_style = lcs)
  31. my_config = pygal.Config()
  32. my_config.x_label_rotation = 45
  33. my_config.show_legend = False
  34. my_config.title_font_size = 24
  35. my_config.label_font_size = 14
  36. my_config.major_label_font_size = 18
  37. my_config.truncate_label = 15
  38. my_config.show_y_guides = False
  39. my_config.width = 1000
  40. chart = pygal.Bar(my_config,style = my_style)
  41. chart.title = “most stared python projects on github”
  42. chart.x_labels= names
  43. chart.add(,plot_dicts)
  44. 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'])

搞定。

效果:

from:https://blog.csdn.net/qq_15514565/article/details/77688676

原文链接:,转发请注明来源!
评论已关闭。