无法使用 LXML 通过标记在 XML 中找到元素
Couldn' find element in XML by tag using LXML
尝试在 this 之后用 Python 3.6.8
和 LXML 4.4.1
解析 data.xspf
并找到 <creator>Creator</creator>
元素但有 []
输出。
data.xspf
<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="http://xspf.org/ns/0/" version="1">
<title/>
<creator/>
<trackList>
<track>
<location>http://localhost:8000</location>
<creator>Creator</creator>
<title>Title</title>
<annotation>Blah
Blah
Blah
Blah
Blah
Blah
Blah</annotation>
<info>info</info>
</track>
</trackList>
</playlist>
脚本:
>>> from lxml import etree
>>> tree = etree.parse("data.xspf")
>>> tree.findall('.//creator')
有什么想法吗?
使用函数的第二个参数确保考虑默认命名空间tree.findall('.//creator', { None : 'http://xspf.org/ns/0/' })
。
尝试在 this 之后用 Python 3.6.8
和 LXML 4.4.1
解析 data.xspf
并找到 <creator>Creator</creator>
元素但有 []
输出。
data.xspf
<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="http://xspf.org/ns/0/" version="1">
<title/>
<creator/>
<trackList>
<track>
<location>http://localhost:8000</location>
<creator>Creator</creator>
<title>Title</title>
<annotation>Blah
Blah
Blah
Blah
Blah
Blah
Blah</annotation>
<info>info</info>
</track>
</trackList>
</playlist>
脚本:
>>> from lxml import etree
>>> tree = etree.parse("data.xspf")
>>> tree.findall('.//creator')
有什么想法吗?
使用函数的第二个参数确保考虑默认命名空间tree.findall('.//creator', { None : 'http://xspf.org/ns/0/' })
。