最近在分析Github的Python开源项目时候遇到了一些问题,直接上传代码段:

import requests

import pygal

from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS

# 执行API调用并存储

url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'

r = requests.get(url)

print("Status code:", r.status_code)

# 将API响应存储在一个变量里面

response_dict = r.json()

print("Total repositories:", response_dict['total_count'])

# 探索有关仓库的信息

repo_dicts = response_dict['items']

print("Repositories returned:", len(repo_dicts))

# # 研究第一个仓库

# repo_dict = repo_dicts[0]

#

# # 打印各种信息

# print("\nSelected information about first repository:")

# for repo_dict in repo_dicts:

# print('Name:', repo_dict['name'])

# print('Owner:', repo_dict['owner']['login'])

# print('Stars:', repo_dict['stargazers_count'])

# print('Repository:', repo_dict['html_url'])

# print('Created:', repo_dict['created_at'])

# print('Updated:', repo_dict['updated_at'])

# print('Description', repo_dict['description'])

names, plot_dicts = [], []

for repo_dict in repo_dicts:

names.append(repo_dict['name'])

plot_dict = dict(value=repo_dict['stargazers_count'], label=repo_dict['description'])

plot_dicts.append(plot_dict)

# 可视化

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-Starred Python Projects on Github'

chart.x_labels = names

chart.add('', plot_dicts)

chart.render_to_file('python_repos.svg')

运行上述代码,遇到了如下错误:

/usr/bin/python3.5 /home/linuxer/code/date_visual/python_repos.py

Status code: 200

Total repositories: 1995979

Repositories returned: 30

Traceback (most recent call last):

File "/home/linuxer/code/date_visual/python_repos.py", line 58, in

chart.render_to_file('python_repos.svg')

File "/usr/local/lib/python3.5/dist-packages/pygal/graph/public.py", line 114, in render_to_file

f.write(self.render(is_unicode=True, **kwargs))

File "/usr/local/lib/python3.5/dist-packages/pygal/graph/public.py", line 52, in render

self.setup(**kwargs)

File "/usr/local/lib/python3.5/dist-packages/pygal/graph/base.py", line 217, in setup

self._draw()

File "/usr/local/lib/python3.5/dist-packages/pygal/graph/graph.py", line 933, in _draw

self._plot()

File "/usr/local/lib/python3.5/dist-packages/pygal/graph/bar.py", line 146, in _plot

self.bar(serie)

File "/usr/local/lib/python3.5/dist-packages/pygal/graph/bar.py", line 116, in bar

metadata)

File "/usr/local/lib/python3.5/dist-packages/pygal/util.py", line 233, in decorate

metadata['label'])

File "/usr/local/lib/python3.5/dist-packages/pygal/_compat.py", line 61, in to_unicode

return string.decode('utf-8')

AttributeError: 'NoneType' object has no attribute 'decode'

当时猜测这个原因是可能有个模块有问题。

但是后来发现不会是这个原因,结合错误 'NoneType' object has no attribute 'decode' ,应该是某个变量没有指明属性。

于是尝试加上 str() 尝试,将其中 label=repo_dict['description'] 替换为label=str(repo_dict['description']) .

于是修正后代码,重新编译:

/usr/bin/python3.5 /home/linuxer/code/date_visual/python_repos.py

Status code: 200

Total repositories: 1995996

Repositories returned: 30

Process finished with exit code 0

成功编译成功:

于是就可以继续进行下一步的数据分析。

有些地方属性没指明,也可能是空值,所以要对其进行处理,不然会遇到错误。

谢谢各位关注。中秋节快乐!

pythongetattribute_对Github上Python开源项目进行分析时遇到的一个AttributeError的解释及其解决方法。...相关推荐

  1. GitHub上Swift开源项目!

    swift-open-project 这里汇集了目前为止最为流行的Swift开源项目,选取SwiftGuide中的开源部分,这里将每周对项目进行总结,如果大家有推荐的可以联系thinkloki@gma ...

  2. github上的开源项目中gif图片的制作软件

    在github上学习开源项目的时候,看到好些开源项目里的示例图片都是gif格式的,这些gif图片能够很好地介绍此开源项目的功能,尤其是在电脑上运行android模拟器时,然后就想着自己以后做开源的时候 ...

  3. 人工智能科技成熟的11个Github上免费开源项目,很多电影中才有的场景应用到现实颠覆普通人的认知和想象

    人工智能科技成熟的11个Github上免费开源项目,很多电影中才有的场景应用到现实颠覆普通人的认知和想象. 全文大纲: PULSE - 该开源项目可以通过给图片增加像素点来实现去马赛克或高清化. De ...

  4. 机器人伪装成人类在 GitHub 上为开源项目修复 bug

    2018年1月12日下午12:28,GeoWebCache/geowebcache 项目和往常一样在 github 上提交代码.2 分钟后 travis-ci 上的单元测试以失败告终,测试结果显示有 ...

  5. GitHub上12306开源项目如何本地启动,注意:不需要Python基础

    这马上过年的,无意间发现GitHub上有一个开源的12306抢票工具,但是遗憾的是,该项目为python的项目,考虑到本人并无python基础,所以自己鼓捣了一天,总算可以将项目跑起来了.下面我来带领 ...

  6. 九价抢不到?多试试这几个GitHub上的开源项目

    大家好,我是TJ 关注TJ君,回复"武功秘籍"免费获取计算机宝典书籍 随着天气寒冷,似乎国内的疫情又有反复之势,尤其是西安整座城市的严峻,让人们清楚的意识到,防疫之路任重道远. 今 ...

  7. github怎么搜索开源的代码_5.2.PYTHON开发利器之使用VS Code+GitHub进行python开源项目开发...

    上一节介绍了使用vscode+Git进行python程序版本控制,详见5.1.PYTHON开发利器之使用VS Code+Git进行python程序版本控制 0.前言 GitHub是目前最大的开源代码托 ...

  8. Android github上优秀开源项目分类汇总

    第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...

  9. github上Android开源项目汇总(带效果图)

    第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...

最新文章

  1. python实训收获_英泰移动通信:学习Python心得体会
  2. parse() got an unexpected keyword argument 'transport_encoding'
  3. Android移动开发之【Android实战项目】Textview各项属性(全)
  4. 【Python】pip工具使用知识,模型保存pickle,PDF与docx相互转换处理
  5. SCARA四轴机器人丝杆花键_花键丝杆一体轴型SCARA机器人的制作方法
  6. ❤JavaScript系列6部曲:语法篇(万字长文)❤
  7. NVIDIA Jetson Nano B01 安装Ubuntu 18.04.3 LTS
  8. 微信公众平台体验(二)(单词翻译)
  9. python selenium框架_python+selenium之框架设计
  10. 汇编试验四:[bx] 和 loop 的使用
  11. shader变体是什么_shader 里面的分支
  12. mybatisplus activerecord之mybatisplus entity XXX Not Found TableInfoCache.错误
  13. java读取txt文件---,java读取TXT文件的方法
  14. 期末复习—计算机组成原理—计算机的分类
  15. 英语词根词缀记忆法(全集)_【干货又来啦】中考英语词汇速记,你也会说 so easy啦...
  16. API v3版微信支付平台证书获取与自动更新
  17. JavaSE Lambda 表达式
  18. (转)Unity十大插件
  19. 【web前端开发 | CSS】页面布局之盒子模型
  20. 数字图像处理笔记——阈值(Thresholding)

热门文章

  1. Python-基础知识-常用模块
  2. 如何利用一台pc获取百万利益 《标题党》
  3. Windows 之 win10快捷键
  4. 关于网页显示乱码问题的一些个人见解(PHP、JSP...)
  5. 动态规划 4、基础背包问题总结(多重背包与多重背包的转化)
  6. 在线网上打字系统_在线网上打字比赛软件_打字练习_中英文打字系统
  7. 【H5】解决ios禁止缩放失效的方法
  8. BASE64Encoder cannot be resolved to a type类似问题的解决办法
  9. python 实现异常退出
  10. mysql 远程连接速度慢的解决方案