dar*_*dar 5 python beautifulsoup python-2.7
我正在使用这样的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
Run Code Online (Sandbox Code Playgroud)
有没有办法指定命名空间,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'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
694 次 |
| 最近记录: |