如何使用 BeautifulSoup 访问命名空间 XML 元素? [英] How can I access namespaced XML elements using BeautifulSoup?
问题描述
我有一个 XML 文档,内容如下:
I have an XML document which reads like this:
<xml>
<web:Web>
<web:Total>4000</web:Total>
<web:Offset>0</web:Offset>
</web:Web>
</xml>
我的问题是如何在 python 中使用像 BeautifulSoup 这样的库来访问它们?
my question is how do I access them using a library like BeautifulSoup in python?
xmlDom.web["Web"].Total ?不起作用?
xmlDom.web["Web"].Total ? does not work?
推荐答案
BeautifulSoup is'一个 DOM 库本身(它没有实现 DOM API).更复杂的是,您在该 xml 片段中使用了名称空间.要解析特定的 XML 片段,您可以按如下方式使用 BeautifulSoup:
BeautifulSoup isn't a DOM library per se (it doesn't implement the DOM APIs). To make matters more complicated, you're using namespaces in that xml fragment. To parse that specific piece of XML, you'd use BeautifulSoup as follows:
from BeautifulSoup import BeautifulSoup
xml = """<xml>
<web:Web>
<web:Total>4000</web:Total>
<web:Offset>0</web:Offset>
</web:Web>
</xml>"""
doc = BeautifulSoup( xml )
print doc.find( 'web:total' ).string
print doc.find( 'web:offset' ).string
如果您没有使用命名空间,代码可能如下所示:
If you weren't using namespaces, the code could look like this:
from BeautifulSoup import BeautifulSoup
xml = """<xml>
<Web>
<Total>4000</Total>
<Offset>0</Offset>
</Web>
</xml>"""
doc = BeautifulSoup( xml )
print doc.xml.web.total.string
print doc.xml.web.offset.string
这里的关键是 BeautifulSoup 对命名空间一无所知(或关心).因此,web:Web
被视为 web:web
标签,而不是属于 eweb
的 Web
标签> 命名空间.虽然 BeautifulSoup 将 web:web
添加到 xml 元素字典中,但 Python 语法不会将 web:web
识别为单个标识符.
The key here is that BeautifulSoup doesn't know (or care) anything about namespaces. Thus web:Web
is treated like a web:web
tag instead of as a Web
tag belonging to th eweb
namespace. While BeautifulSoup adds web:web
to the xml element dictionary, python syntax doesn't recognize web:web
as a single identifier.
您可以通过阅读文档了解更多相关信息.
You can learn more about it by reading the documentation.
这篇关于如何使用 BeautifulSoup 访问命名空间 XML 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!