使用 LXML 解析 Python 中的 XML
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
这是我目前使用的代码。问题是,我需要找出 'current' leagueid,因为在解析该联赛中的事件时需要它。
leagueid = tree.xpath('//id/text()')
returns 所有联盟的列表,而不仅仅是 'current one'
我希望我的解释是正确的,有人可以给我一个 hand/advice。
XML 文档:http://pastebin.com/BDaJ7Ayx
我认为这是从 leagues
变量引用的当前节点获取 id 所需要的:
leagueid = leagues.xpath('./id/text()')
以上 Xpath 从当前 <league>
节点寻找子节点 <id>
。
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
这是我目前使用的代码。问题是,我需要找出 'current' leagueid,因为在解析该联赛中的事件时需要它。
leagueid = tree.xpath('//id/text()')
returns 所有联盟的列表,而不仅仅是 'current one'
我希望我的解释是正确的,有人可以给我一个 hand/advice。
XML 文档:http://pastebin.com/BDaJ7Ayx
我认为这是从 leagues
变量引用的当前节点获取 id 所需要的:
leagueid = leagues.xpath('./id/text()')
以上 Xpath 从当前 <league>
节点寻找子节点 <id>
。