How to extract xml attribute using Python ElementTree

前端 未结 4 741
一向
一向 2020-12-08 13:39

For:


 text

How do I get \"value\"?

xml.findtext(\"./bar[@key]         


        
4条回答
  •  醉话见心
    2020-12-08 14:15

    This will find the first instance of an element named bar and return the value of the attribute key.

    In [52]: import xml.etree.ElementTree as ET
    
    In [53]: xml=ET.fromstring(contents)
    
    In [54]: xml.find('./bar').attrib['key']
    Out[54]: 'value'
    

提交回复
热议问题