Python Lxml (objectify): Checking whether a tag exists

后端 未结 4 1339
后悔当初
后悔当初 2021-02-02 11:48

I need to check whether a certain tag exists in an xml file.

For example, I want to see if the tag exists in this snippet:

 
4条回答
  •  再見小時候
    2021-02-02 12:14

    Assume you want to get elem2's value, you can use xpath to find it.

    tree = etree.parse(StringIO(htmlString), etree.HTMLParser()).getroot()
    youWantValue = tree.xpath('/main/elem2')[0].text
    

提交回复
热议问题