【发布时间】:2015-08-21 07:42:09
【问题描述】:
我正在尝试使用BeautifulSoup 解析xml 文件。考虑一个 sampleinpt xml 文件如下:
<DOC>
<DOCNO>1</DOCNO>
....
</DOC>
<DOC>
<DOCNO>2</DOCNO>
....
</DOC>
...
该文件包含 130 个<DOC> 标签。但是,当我尝试使用 BeautifulSoup 的 findAll 函数对其进行解析时,它会检索随机数量的标签(通常在 15 - 25 之间),但从不检索 130。我使用的代码如下:
from bs4 import BeautifulSoup
z = open("filename").read()
soup = BeautifulSoup(z, "lxml")
print len(soup.findAll('doc'))
#more code involving manipulation of results
谁能告诉我我做错了什么?提前致谢!
【问题讨论】:
-
您正在使用 HTML 解析器;为什么不改用
BeautifulSoup(z, 'xml')? -
使用不同的解析器?
标签: python xml python-2.7 parsing beautifulsoup