selecting attribute values from lxml

后端 未结 2 768
一整个雨季
一整个雨季 2020-12-24 02:07

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

I expected the following to work

from lxml import etree

for customer in etree.pa         


        
相关标签:
2条回答
  • 2020-12-24 02:20

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

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

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

    However, you could instead use get:

    print customer.get('NAME')
    

    or attrib:

    print customer.attrib['NAME']
    
    0 讨论(0)
  • 2020-12-24 02:20

    As a possible useful addition, this is how to get the value of an attribute in the case that the element has more than one, and it is the only difference with respect to another element. E.g., given the following file.xml:

    <?xml version ="1.0" encoding="UTF-8"?>
        <level1>
          <level2 first_att='att1' second_att='foo'>8</level2>
          <level2 first_att='att2' second_att='bar'>8</level2>
        </level1>
    

    One can access the attribute 'bar' with:

    import lxml.etree as etree
    tree = etree.parse("test_file.xml")
    print tree.xpath("//level1/level2[@first_att='att2']/@second_att")[0]
    
    0 讨论(0)
提交回复
热议问题