Xpath lxml获取重复子元素的id

2024-05-13 02:09:48 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,这是我的缩写源代码。你知道吗

<meeting>
    <race id="204091" number="1" nomnumber="4" division="0" name="FAIRFIELD RSL CLUB HANDICAP" mediumname="BM90" shortname="BM90" stage="Results" distance="1200" minweight="54" raisedweight="0" class="BM90      " age="3U        " grade="0" weightcondition="HCP       " trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="85000" first="48750" second="16750" third="8350" fourth="4150" fifth="2000" time="2015-08-15T12:35:00" bonustype="BOB7      " nomsfee="0" acceptfee="0" trackcondition="Good 3    " timingmethod="Electronic" fastesttime="1-09.89   " sectionaltime="600/33.95 " formavailable="0" racebookprize="Of $85000. First $48750, second $16750, third $8350, fourth $4150, fifth $2000, sixth $1000, seventh $1000, eighth $1000, ninth $1000, tenth $1000">
    <race id="204092" number="2" nomnumber="5" division="0" name="DOOLEYS HANDICAP" mediumname="BM80" shortname="BM80" stage="Results" distance="1500" minweight="54" raisedweight="0" class="BM80      " age="3U        " grade="0" weightcondition="HCP       " trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="85000" first="48750" second="16750" third="8350" fourth="4150" fifth="2000" time="2015-08-15T13:15:00" bonustype="BOB7      " nomsfee="0" acceptfee="0" trackcondition="Good 3    " timingmethod="Electronic" fastesttime="1-30.01   " sectionaltime="600/34.27 " formavailable="0" racebookprize="Of $85000. First $48750, second $16750, third $8350, fourth $4150, fifth $2000, sixth $1000, seventh $1000, eighth $1000, ninth $1000, tenth $1000">
    <race id="204093" number="3" nomnumber="2" division="0" name="CANTERBURY HURLSTONE PARK RSL CLUB SPRING PREVIEW" mediumname="SPRINGPREV" shortname="SPRINGPREV" stage="Results" distance="1400" minweight="54" raisedweight="0" class="~         " age="3U        " grade="0" weightcondition="HCP       " trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="85000" first="48750" second="16750" third="8350" fourth="4150" fifth="2000" time="2015-08-15T13:50:00" bonustype="BOB7      " nomsfee="0" acceptfee="0" trackcondition="Good 3    " timingmethod="Electronic" fastesttime="1-24.61   " sectionaltime="600/33.06 " formavailable="0" racebookprize="Of $85000. First $48750, second $16750, third $8350, fourth $4150, fifth $2000, sixth $1000, seventh $1000, eighth $1000, ninth $1000, tenth $1000">
</meeting>

我的代码为每个race元素循环正确的次数,但是它不会将id的检索移到下一个元素。如何相对于循环移动树?你知道吗

from lxml import objectify, etree
from builtins import len

with open('20150815RHIL0.xml', 'r') as f:
    xml = f.read()
    root = objectify.fromstring(xml)

    #===========================================================================
    # print(root.tag)
    # print(root.attrib['date'], root.attrib['weather'])
    # print(root.race.attrib['id'])
    #===========================================================================


    for races in root.race.iter():
        print(root.race.attrib['id'])

Tags: nameidnumberrootdivisionprintsecondrace
2条回答

只是因为您没有得到正在迭代的当前对象元素,它应该是:

root = objectify.fromstring(xml)

for race in root.meeting.iter('race'):
    print(race.get('id'))

另外,根据xml片段,root.race不是有效路径,应该是root.meeting.race。因为您想要获取所有种族元素,所以:root.meeting.iter('race')

您需要在循环中使用循环变量。替换:

for races in root.race.iter():
    print(root.race.attrib['id'])

使用:

for race in root.race.iter():
    print(race.attrib['id'])

相关问题 更多 >