<legend id='zg3aY'><style id='zg3aY'><dir id='zg3aY'><q id='zg3aY'></q></dir></style></legend>

      1. <i id='zg3aY'><tr id='zg3aY'><dt id='zg3aY'><q id='zg3aY'><span id='zg3aY'><b id='zg3aY'><form id='zg3aY'><ins id='zg3aY'></ins><ul id='zg3aY'></ul><sub id='zg3aY'></sub></form><legend id='zg3aY'></legend><bdo id='zg3aY'><pre id='zg3aY'><center id='zg3aY'></center></pre></bdo></b><th id='zg3aY'></th></span></q></dt></tr></i><div id='zg3aY'><tfoot id='zg3aY'></tfoot><dl id='zg3aY'><fieldset id='zg3aY'></fieldset></dl></div>

          <bdo id='zg3aY'></bdo><ul id='zg3aY'></ul>
        <tfoot id='zg3aY'></tfoot>

      2. <small id='zg3aY'></small><noframes id='zg3aY'>

      3. 从 lxml 中选择属性值

        时间:2023-09-13

        <tfoot id='Ky6Em'></tfoot>

              <tbody id='Ky6Em'></tbody>

          • <i id='Ky6Em'><tr id='Ky6Em'><dt id='Ky6Em'><q id='Ky6Em'><span id='Ky6Em'><b id='Ky6Em'><form id='Ky6Em'><ins id='Ky6Em'></ins><ul id='Ky6Em'></ul><sub id='Ky6Em'></sub></form><legend id='Ky6Em'></legend><bdo id='Ky6Em'><pre id='Ky6Em'><center id='Ky6Em'></center></pre></bdo></b><th id='Ky6Em'></th></span></q></dt></tr></i><div id='Ky6Em'><tfoot id='Ky6Em'></tfoot><dl id='Ky6Em'><fieldset id='Ky6Em'></fieldset></dl></div>

                <legend id='Ky6Em'><style id='Ky6Em'><dir id='Ky6Em'><q id='Ky6Em'></q></dir></style></legend>
              1. <small id='Ky6Em'></small><noframes id='Ky6Em'>

                  <bdo id='Ky6Em'></bdo><ul id='Ky6Em'></ul>
                • 本文介绍了从 lxml 中选择属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I want to use an xpath expression to get the value of an attribute.

                  我希望以下工作

                  from lxml import etree
                  
                  for customer in etree.parse('file.xml').getroot().findall('BOB'):
                      print customer.find('./@NAME')
                  

                  但这给出了一个错误:

                  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: '@'
                  

                  我期望这会起作用吗?

                  推荐答案

                  findfindall 只实现 XPath 的一个子集.它们的存在是为了提供与其他 ElementTree 实现(如 ElementTreecElementTree)的兼容性.

                  find and findall only implement a subset of XPath. Their presence is meant to provide compatibility with other ElementTree implementations (like ElementTree and cElementTree).

                  相比之下,xpath 方法提供对 XPath 1.0 的完全访问:

                  The xpath method, in contrast, provides full access to XPath 1.0:

                  print customer.xpath('./@NAME')[0]
                  

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

                  However, you could instead use get:

                  print customer.get('NAME')
                  

                  属性:

                  print customer.attrib['NAME']
                  

                  这篇关于从 lxml 中选择属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 类动态属性访问 下一篇:python属性获取和设置顺序是什么?

                  相关文章

                  最新文章

                  1. <i id='su4A3'><tr id='su4A3'><dt id='su4A3'><q id='su4A3'><span id='su4A3'><b id='su4A3'><form id='su4A3'><ins id='su4A3'></ins><ul id='su4A3'></ul><sub id='su4A3'></sub></form><legend id='su4A3'></legend><bdo id='su4A3'><pre id='su4A3'><center id='su4A3'></center></pre></bdo></b><th id='su4A3'></th></span></q></dt></tr></i><div id='su4A3'><tfoot id='su4A3'></tfoot><dl id='su4A3'><fieldset id='su4A3'></fieldset></dl></div>

                    <legend id='su4A3'><style id='su4A3'><dir id='su4A3'><q id='su4A3'></q></dir></style></legend>

                  2. <tfoot id='su4A3'></tfoot>
                        <bdo id='su4A3'></bdo><ul id='su4A3'></ul>
                    1. <small id='su4A3'></small><noframes id='su4A3'>