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)
问题是什么?我怎么解决这个问题?
答案 0 :(得分:2)
这里:
else:
news[child.tag] = child.text.encode('utf8')
在某些情况下, child.text
为None
。因此,在这种情况下不要创建dict条目,例如:
elif child.text is not None:
news[child.tag] = child.text.encode('utf8')