【发布时间】:2011-06-30 08:10:07
【问题描述】:
让我举个例子:
from BeautifulSoup import BeautifulStoneSoup
root = ''' <all2>
<images>
<image>
<name> Picture </name>
<url> www.thing.com</url>
</image>
<image>
<name> Another one! </name>
</image>
</images>
</all2>
'''
soup = BeautifulStoneSoup(root)
for img in soup.all2.images.findAll("image"):
iname = img.i_name
iurl = img.url
print iname
print iurl
让标签是可选的。在这种情况下,第二次迭代将找不到标签,并抛出异常:
AttributeError: 'NoneType' 对象没有属性 'renderContents'
如果没有出现可选标签,我希望 iurl 为 None。这可能吗?还是我的 XML 理解错误。
【问题讨论】:
标签: python xml beautifulsoup