for item in root.findall('./channel/item'):
news = {}
# iterate child elements of item
for child in item:
# special checking for namespace object content:media
if child.tag == '{http://search.yahoo.com/mrss/}content':
news['media'] = child.attrib['url']
else:
news[child.tag] = child.text.encode('utf8')
newsitems.append(news)
有什么问题吗?我该如何解决这个问题?
最佳答案
这里:
else:
news[child.tag] = child.text.encode('utf8')
在某些情况下,child.text
为None
。因此,在这种情况下不要创建字典条目,例如这样:
elif child.text is not None:
news[child.tag] = child.text.encode('utf8')
关于python-2.7 - 属性错误: 'NoneType' object has no attribute 'encode' in python 2. 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322223/