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

最近在分析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

成功编译成功:

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值