我执行以下操作:
from BeautifulSoup import *
html = u'<body><b>In Body<b>Second level</b></b></body>'
soup = BeautifulSoup(html)
soup.contents
结果我得到:
[<body><b>In Body</b><b>Second level</b></body>]
这对我来说看起来很奇怪,因为我看不到原始的 XML。本来我有一个标签<b>
包含一些文本( In Body
),然后包含另一个标签 <b>
。然而,BeautifulSoup
“认为”我有标签 <b>
在它之后(关闭之后)我有另一个标签 <b>
。因此,标签不会被视为相互嵌套。这是为什么?
已添加
对于那些提示我的示例中 HTML 有效性的人,我做了以下示例:
xml = u'<aaa><bbb>In Body<bbb>Second level</bbb></bbb></aaa>'
soup = BeautifulSoup(xml)
soup.contents
返回:
[<aaa><bbb>In Body</bbb><bbb>Second level</bbb></aaa>]
添加2
如果我使用:
xml = u'<body><b>In Body<b>Second level</b></b></body>'
soup = BeautifulSoup(xml, ['lxml', 'xml'])
我得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/BeautifulSoup.py", line 1522, in __init__
BeautifulStoneSoup.__init__(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/BeautifulSoup.py", line 1147, in __init__
self._feed(isHTML=isHTML)
File "/usr/local/lib/python2.7/dist-packages/BeautifulSoup.py", line 1189, in _feed
SGMLParser.feed(self, markup)
File "/usr/lib/python2.7/sgmllib.py", line 104, in feed
self.goahead(0)
File "/usr/lib/python2.7/sgmllib.py", line 138, in goahead
k = self.parse_starttag(i)
File "/usr/lib/python2.7/sgmllib.py", line 296, in parse_starttag
self.finish_starttag(tag, attrs)
File "/usr/lib/python2.7/sgmllib.py", line 338, in finish_starttag
self.unknown_starttag(tag, attrs)
File "/usr/local/lib/python2.7/dist-packages/BeautifulSoup.py", line 1344, in unknown_starttag
and (self.parseOnlyThese.text or not self.parseOnlyThese.searchTag(name, attrs)):
AttributeError: 'list' object has no attribute 'text'
最佳答案
请注意,您正在使用过时的软件包 BeautifulSoup
:
This package is OBSOLETE. It has been replaced by the beautifulsoup4 package. You should use Beautiful Soup 4 for all new projects
BeautifulSoup 3 包含一些 XML 解析功能(BeautifulStoneSoup
),这些功能确实无法理解再次嵌套相同的标记(如 his answer 中的 7stud 所指出的;因此,对于所有 XML 解析需求,它应该完全考虑被 BeautifulSoup 4 取代。请注意,这些包甚至可以在一个应用程序中共存 - 用于 BS3 的 BeautifulSoup.BeautifulSoup
和用于 BS4 的 bs4.BeautifulSoup
。 p>
BeautifulSoup 4默认使用HTML规则进行解析;您需要明确告诉它使用 XML(需要安装 lxml
)。因此,以 BeautifulSoup 4 为例(PyPI beautifulsoup4
):
>>> from bs4 import BeautifulSoup
>>> xml = u'<body><b>In Body<b>Second level</b></b></body>'
>>> soup = BeautifulSoup(xml, 'xml')
>>> soup.contents
[<body><b>In Body<b>Second level</b></b></body>]
>>> bs4.__version__
'4.1.3'
请注意,文档必须是格式良好的 XML;绝不手软。
如果不使用'xml'
参数,您将得到错误解析的文档:
>>> bs4.BeautifulSoup('<p><p></p></p>')
<html><body><p></p><p></p></body></html>
并与
>>> bs4.BeautifulSoup('<p><p></p></p>', 'xml')
<?xml version="1.0" encoding="utf-8"?>
<p><p/></p>
关于python - 为什么 BeautifulSoup 重新格式化我的 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28655242/