2017-07-26 33 views
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) 

whats問題?どのように私はこの問題を解決するつもりですか?ここAttributeError: 'NoneType'オブジェクトには、Pythonで 'エンコード'属性がありません2.7

+0

これは 'child.text'が' None'であるためです。それをフィルタリングしてください。 'if child.text:... ' –

答えて

0

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') 
+0

ありがとう!!出来た –

関連する問題