Eci*_*ana 4 python xml elementtree
我有一个有效的 XHTML 文件。当我做
import xml.etree.ElementTree as ET
print ET._namespace_map
Run Code Online (Sandbox Code Playgroud)
它列出了:
'http://www.w3.org/1999/xhtml': 'html'
Run Code Online (Sandbox Code Playgroud)
当我做:
root.find('{http://www.w3.org/1999/xhtml}head')
Run Code Online (Sandbox Code Playgroud)
它发现:
<Element '{http://www.w3.org/1999/xhtml}head' at 0x104647168>
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时:
root.find('html:head')
Run Code Online (Sandbox Code Playgroud)
它抱怨:
SyntaxError: prefix 'html' not found in prefix map
Run Code Online (Sandbox Code Playgroud)
是否可以find使用语法找到名称间隔元素ns:element?
您应该指定namespaces参数:
namespaces = {'html': 'http://www.w3.org/1999/xhtml'}
root.find('html:head', namespaces=namespaces)
Run Code Online (Sandbox Code Playgroud)
另见: