从lxml中选择属性值

GHZ*_*GHZ 44 python lxml

我想使用xpath表达式来获取属性的值.

我期望以下工作

from lxml import etree

for customer in etree.parse('file.xml').getroot().findall('BOB'):
    print customer.find('./@NAME')
Run Code Online (Sandbox Code Playgroud)

但这会给出一个错误:

Traceback (most recent call last):
  File "bob.py", line 22, in <module>
    print customer.find('./@ID')
  File "lxml.etree.pyx", line 1409, in lxml.etree._Element.find (src/lxml/lxml.etree.c:39972)
  File "/usr/local/lib/python2.7/dist-packages/lxml/_elementpath.py", line 272, in find
    it = iterfind(elem, path, namespaces)
  File "/usr/local/lib/python2.7/dist-packages/lxml/_elementpath.py", line 262, in iterfind
    selector = _build_path_iterator(path, namespaces)
  File "/usr/local/lib/python2.7/dist-packages/lxml/_elementpath.py", line 246, in _build_path_iterator
    selector.append(ops[token[0]](_next, token))
KeyError: '@'
Run Code Online (Sandbox Code Playgroud)

我错在期待这个工作吗?

unu*_*tbu 56

find并且findall 只实现 XPath 的子集.它们的存在旨在提供与其他ElementTree实现(如ElementTreecElementTree)的兼容性.

xpath相反,该方法提供对XPath 1.0的完全访问权限:

print customer.xpath('./@NAME')[0]
Run Code Online (Sandbox Code Playgroud)

但是,您可以改为使用get:

print customer.get('NAME')
Run Code Online (Sandbox Code Playgroud)

或者attrib:

print customer.attrib['NAME']
Run Code Online (Sandbox Code Playgroud)

  • 正确,但如果你想要"官方"首选方式:使用`customer.get('NAME')`(参见http://docs.python.org/library/xml.etree.elementtree.html#xml.etree. ElementTree.Element.attrib) (7认同)