简体   繁体   中英

Parsing XML in Python using LXML

tree = etree.parse("pinnacle_feed.xml")

fdtime = tree.xpath('//rsp/fd/fdTime/text()')
global lasttime 
lasttime = fdtime[0]


for leagues in tree.getiterator('league'):
    leagueid = tree.xpath('//id/text()')

    for elt in leagues.getiterator('event'):
        startDateTime = elt.xpath('//startDateTime/text()')
        eventId = elt.xpath('//id/text()')
        homeTeam = elt.xpath('./homeTeam/name/text()')
        awayTeam = elt.xpath('./awayTeam/name/text()')
        homeTeamOdds = elt.xpath('./periods/period/moneyLine/homePrice/text()')
        awayTeamOdds = elt.xpath('./periods/period/moneyLine/awayPrice/text()')
        drawOdds = elt.xpath('./periods/period/moneyLine/drawPrice/text()')
        print full_iterator

That is the code I am currently using. The issue is, I need to find out the 'current' leagueid as it is needed when I parse through the events in that league.

leagueid = tree.xpath('//id/text()') 

returns a list of all the leagueids and not just the 'current one'

I hope I explained myself correctly and someone could give me a hand/advice.

XML doc: http://pastebin.com/BDaJ7Ayx

I think this is what you need to get the id from current node referenced by leagues variable :

leagueid = leagues.xpath('./id/text()')

Above Xpath looks for child node <id> from current <league> node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM