2011-07-14 1 views
2

この部分のコードは、XMLを解析してWebページの画面に出力します。 XMLのPython CGIスクリプト(XMLおよびmindomを使用)がnullデータを抽出できない

AttributeError: 'NoneType' object has no attribute 'toxml' 
     args = ("'NoneType' object has no attribute 'toxml'",) 

スニペットが解析されている:ページはエラーメッセージとはページのタイトルや説明を、持っていない場合、それはヌルXMLタグのセット、すなわちに遭遇した場合

for counter in range(100): 
    try: 
     for item in BlekkoSearchResultsXML.getElementsByTagName('item'): 
      Blekko_PageTitle = item.getElementsByTagName('title')[counter].firstChild.toxml(encoding="utf-8") 
      Blekko_PageDesc = item.getElementsByTagName('description')[counter].firstChild.toxml(encoding="utf-8") 
      Blekko_DisplayURL = item.getElementsByTagName('guid')[counter].firstChild.toxml(encoding="utf-8") 
      Blekko_URL = item.getElementsByTagName('link')[counter].firstChild.toxml(encoding="utf-8") 
      print "<h2>" + Blekko_PageTitle + "</h2>" 
      print Blekko_PageDesc + "<br />" 
      print Blekko_DisplayURL + "<br />" 
      print Blekko_URL + "<br />" 
    except IndexError: 
     break 

しかし、スクリプトが失敗します:

<item> 
     <title>SUSHI FANLISTING</title> 
     <link>http://sushi.perfectdrug.net/</link> 
     <guid>http://sushi.perfectdrug.net/</guid> 
     <description>This is the official...</description> 
     </item> 

私は失敗し、次のようにtry/except文を使って試してみました:

try: 
    Blekko_PageTitle = item.getElementsByTagName('title')[counter].firstChild.toxml(encoding="utf-8") 
except Blekko_PageTitle = None: 
    Blekko_PageTitle = "No page title provided..." 

感謝の意を表します。

答えて

0

あなたはしていますexcept間違って:それは発生する例外オブジェクトをキャッチします。

使用し
if Blekko_PageTitle = None: 
    ... 
else: 
    ... 
+0

:あなたは条件付きの使用、また

except AttributeError: 

をしたい 'はAttributeErrorを除い:' を、完全に働きました。どうもありがとう。 – user725236

関連する問題