这部分代码解析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
但是,如果脚本遇到一组空XML标记,即如果页面没有页面标题或描述,则脚本失败,并显示错误消息:
AttributeError: 'NoneType' object has no attribute 'toxml'
args = ("'NoneType' object has no attribute 'toxml'",)
正在解析的XML代码片段:
<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..."
你做的except
是错误的:它捕获被引发的Exception对象。你想要
except AttributeError:
或者,使用条件语句:
if Blekko_PageTitle = None:
...
else:
...