selecting attribute values from lxml

后端 未结 2 779
一整个雨季
一整个雨季 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']
    

提交回复
热议问题