【发布时间】:2013-06-13 09:57:19
【问题描述】:
我有一个文件,其中包含用格式良好的 XML 包装的句子(xmllint 和 tidylib 是这样说的)。 所以xml看起来像这样:
<a id="100" attr1="text" attr1="text" attr1="text">
<tagname id="1">
This is my sentence.
</tagname>
</a>
<a id="101" attr1="text" attr1="text" attr1="text">
<tagname id="1">
This is my sentence.
</tagname>
</a>
等等。
我使用下面的代码提取带有属性的句子(本例中从id 1到85)
a1 = open(r"file.xml",'r')
a = a1.readlines()
a1.close()
soup = BeautifulSoup(str(a))
for i in range(1,85):
a = soup.find('a', {'id': i})
achild = a.find('tagname')
tagnametext = achild.contents
print tagnametext
一切都打印得很好,直到第 84 句我收到错误: achild = a.find('tagname') AttributeError: 'NoneType' 对象没有属性 'find'
每组 ... 都是用 for 循环生成的,所以 xml 都是一样的。 我尝试过使用不同数量的句子的不同文件。发生错误的 id 也会发生变化。 这是beautifulsoup的限制吗? 它不能扫描超过一定数量的行?
【问题讨论】:
-
id 84 长什么样子?
标签: python xml beautifulsoup