I wrote this tiny Python snippet that scrapes a feed and prints it out. When I run the code, something in the feed triggers the error message you see here as my question. Here's the complete console output on error:
我写了这个微小的Python片段,它抓取一个feed并将其打印出来。当我运行代码时,Feed中的某些内容会触发您在此处看到的错误消息作为我的问题。这是错误的完整控制台输出:
> Traceback (most recent call last):
> File "/home/vijay/ffour/ffour5.py",
> line 20, in <module>
> myfeed() File "/home/vijay/ffour/ffour5.py", line
> 15, in myfeed
> sys.stdout.write(entry["title"]).encode('utf-8')
> AttributeError: 'NoneType' object has
> no attribute 'encode'
2 个解决方案
#1
12
> sys.stdout.write(entry["title"]).encode('utf-8')
This is the culprit. You probably mean:
这是罪魁祸首。你可能意味着:
sys.stdout.write(entry["title"].encode('utf-8'))
(Notice the position of the last closing bracket.)
(注意最后一个右括号的位置。)
#2
5
Lets try to clear up some of the confusion in the exception message.
让我们尝试清除异常消息中的一些混淆。
The function call
函数调用
sys.stdout.write(entry["title"])
Returns None. The ".encode('utf-8')" is a call to the encode function on what is returned by the above function.
返回None。 “.encode('utf-8')”是对上述函数返回的编码函数的调用。
The problem is that None doesn't have an encode function (or an encode attribute) and so you get an attribute error that names the type you were trying to get an attribute of and the attribute you were trying to get.
问题是None没有编码函数(或编码属性),因此您得到一个属性错误,该错误指出您尝试获取属性的类型以及您尝试获取的属性。
#1
12
> sys.stdout.write(entry["title"]).encode('utf-8')
This is the culprit. You probably mean:
这是罪魁祸首。你可能意味着:
sys.stdout.write(entry["title"].encode('utf-8'))
(Notice the position of the last closing bracket.)
(注意最后一个右括号的位置。)
#2
5
Lets try to clear up some of the confusion in the exception message.
让我们尝试清除异常消息中的一些混淆。
The function call
函数调用
sys.stdout.write(entry["title"])
Returns None. The ".encode('utf-8')" is a call to the encode function on what is returned by the above function.
返回None。 “.encode('utf-8')”是对上述函数返回的编码函数的调用。
The problem is that None doesn't have an encode function (or an encode attribute) and so you get an attribute error that names the type you were trying to get an attribute of and the attribute you were trying to get.
问题是None没有编码函数(或编码属性),因此您得到一个属性错误,该错误指出您尝试获取属性的类型以及您尝试获取的属性。