AttributeError:' NoneType'对象没有属性'编码'在python 2.7中

时间:2017-07-26 09:03:18

标签: python-2.7

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)

问题是什么?我怎么解决这个问题?

1 个答案:

答案 0 :(得分:2)

这里:

    else:
        news[child.tag] = child.text.encode('utf8')
在某些情况下,

child.textNone。因此,在这种情况下不要创建dict条目,例如:

    elif child.text is not None:
        news[child.tag] = child.text.encode('utf8')