0
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)
什麼問題?我該如何解決這個問題?AttributeError:'NoneType'對象在python 2.7中沒有屬性'encode'
這是因爲'child.text'是'None'。過濾出來。 '如果child.text:...' –