簡體   English   中英

如何使用BeautifulSoup4為xml標記指定命名空間?

[英]How do I specify a namespace for an xml tag with BeautifulSoup4?

我正在使用這樣的beautifulsoup4:

from bs4 import BeautifulSoup
xml_string = u"""<something><dcterms:valid><![CDATA[

            start=2012-02-24T00:00:00Z
            end=2030-12-30T00:00:00Z
            scheme=W3C-DTF]]>
        </dcterms:valid></something>"""
soup = BeautifulSoup(xml_string, 'xml')
soup.find('dcterms:valid')  # returns None
soup.find('valid')  # returns the dcterms:valid node

有沒有辦法在soup.find(tagname)指定命名空間,所以我可以准確地找到我想找到的內容?

解析時不需要指定'xml'(編輯:除非注釋中有指向的cdata)。

以下是適合我的示例代碼

>>> soup = BeautifulSoup(xml_string)
>>> soup.find('valid')
>>> soup.find('dcterms:valid')
<dcterms:valid start="2012-02-24T00:00:00Z" end="2030-12-30T00:00:00Z" scheme="W3C-DTF"></dcterms:valid>

>>> item = soup.find('dcterms:valid')
>>> item['start']
u'2012-02-24T00:00:00Z'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM