这部分代码解析 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..."
任何建议表示赞赏。