Parse xml with lxml - extract element value

前端 未结 3 608
有刺的猬
有刺的猬 2020-12-17 19:25

Let\'s suppose we have the XML file with the structure as follows.

 


        
3条回答
  •  情歌与酒
    2020-12-17 20:09

    I would just go with

    for df in doc.xpath('//datafield'):
        print df.attrib
        for sf in df.getchildren():
            print sf.text
    

    Also you don't need urllib, you can directly parse XML with HTTP

    url = "http://dl.dropbox.com/u/540963/short_test.xml"  #doesn't work with https though
    doc = etree.parse(url)
    

提交回复
热议问题