pygal制图“AttributeError: ‘NoneType’ object has no attribute ‘decode’《Python编程:从入门到实践》
最近在刷《Python编程:从入门到实践》一书 webAPI 部分使用github接口绘制柱状图
遇到此报错 通过核对书中代码 发现并无错误
pygal制图“AttributeError: 'NoneType' object has no attribute 'decode'
通过分析是 接口中 description为空原因 ps 原来大神代码也有不健壮的时候啊
完整代码:
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('总数'

在阅读《Python编程:从入门到实践》时,作者在使用pygal绘制柱状图时遇到了'AttributeError: 'NoneType' object has no attribute 'decode''的错误。经过代码审查,发现是由于接口返回的数据为空导致的。解决方案是在代码中为可能为空的部分设置默认值,以增强代码的健壮性。
最低0.47元/天 解锁文章
1522

被折叠的 条评论
为什么被折叠?



