久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 BeautifulSoup 訪問(wèn)命名空間的 XML 元素?

How can I access namespaced XML elements using BeautifulSoup?(如何使用 BeautifulSoup 訪問(wèn)命名空間的 XML 元素?)
本文介紹了如何使用 BeautifulSoup 訪問(wèn)命名空間的 XML 元素?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)這樣的 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>

我的問(wèn)題是如何使用 Python 中的 BeautifulSoup 之類的庫(kù)來(lái)訪問(wèn)它們?

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't 一個(gè) DOM 庫(kù)本身(它不實(shí)現(xiàn) DOM API).更復(fù)雜的是,您在該 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

這里的關(guān)鍵是 BeautifulSoup 對(duì)命名空間一無(wú)所知(或關(guān)心).因此 web:Web 被視為 web:web 標(biāo)記,而不是屬于 ewebWeb 標(biāo)記> 命名空間.雖然 BeautifulSoup 將 web:web 添加到 xml 元素字典中,但 python 語(yǔ)法不會(huì)將 web:web 識(shí)別為單個(gè)標(biāo)識(shí)符.

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.

您可以通過(guò)閱讀文檔了解更多信息.

You can learn more about it by reading the documentation.

這篇關(guān)于如何使用 BeautifulSoup 訪問(wèn)命名空間的 XML 元素?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時(shí)出現(xiàn)問(wèn)題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點(diǎn))
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲(chǔ)在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對(duì)象)
主站蜘蛛池模板: 欧美一区二区三区在线观看 | 国产精品女人久久久 | 欧美精品在线一区 | 久久久国产一区二区三区四区小说 | 三级成人在线观看 | 国产日韩亚洲欧美 | 超碰精品在线 | 爱爱免费视频 | 中文字幕亚洲国产 | 精品自拍视频 | 在线免费观看欧美 | 一二三四在线视频观看社区 | 成人精品一区二区 | 欧美在线 | 在线免费观看黄视频 | 一级女毛片 | 99热这里都是精品 | 久久精品国产亚洲夜色av网站 | 欧美精品一区在线发布 | 亚洲精品一区二区三区 | 黄色片在线观看网址 | 视频在线亚洲 | 欧美一级欧美三级在线观看 | 日韩欧美视频免费在线观看 | 午夜爽爽爽男女免费观看影院 | 一区欧美| 2022精品国偷自产免费观看 | 曰韩三级| 午夜精品| 欧美日韩手机在线观看 | 日韩中文字幕在线观看 | 三级成人在线观看 | 99国产精品久久久 | 久久久久国产精品午夜一区 | 精品久久久久久久 | 视频1区 | 国产精品国产精品国产专区不片 | 欧美在线观看一区 | 午夜欧美一区二区三区在线播放 | 91人人视频在线观看 | www.色五月.com |