我正在尝试使用beautifulsoup解析xml文件并过滤出一个子标记。
我的xml
<meeting>
<race id="5159522" number="1" nomnumber="8" division="0" name="OWNERS RECOGNITION RACEDAY HANDICAP" mediumname="2Y HCP" shortname="2YO HCP" stage="Results" distance="1500" minweight="0.0" raisedweight="0.0" class="~" age="2" grade="0" weightcondition="HCP" trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="100000" first="53375" second="18800" third="8900" fourth="4450" fifth="2475" time="2018-07-28T11:45:00" bonustype="B001" nomsfee="0" acceptfee="0" trackcondition="Good" timingmethod="Electronic" fastesttime="1-32.17" sectionaltime="600/34.36" formavailable="0" racebookprize="Of $100000. First $53375, second $18800, third $8900, fourth $4450, fifth $2475, sixth $2000, seventh $2000, eighth $2000, ninth $2000, tenth $2000">
<condition line="1">Of $100000. First $53375, second $18800, third $8900, fourth $4450, fifth $2475, sixth $2000, seventh $2000, eighth $2000, ninth $2000, tenth $2000</condition>
<condition line="3">No class restriction, Handicap, For Two-Years-Old, No sex restriction</condition>
<condition line="4">BOBS Bonus Available Up To $38500</condition>
<condition line="5">Apprentices can claim.Field Limit: 14 + 4 EM</condition>
<nomination number="1" saddlecloth="1" horse="Thinkin' Big" id="5026846" idnumber="" regnumber="" blinkers="0" trainernumber="12083" trainersurname="" trainerfirstname="" trainertrack="Randwick" rsbtrainername="Gai Waterhouse & Adrian Bott" jockeynumber="541807" jockeysurname="Shinn" jockeyfirstname="Blake" barrier="1" weight="58" rating="0" description="B C 2 High Chaparral(IRE) x Nothin' Leica Cat(NZ) (Tale of the Cat (USA))" colours="White, Bottle Green Band And Sleeves, White And Bottle Green Quartered Cap, White Pom Pom" owners="Dodmark Thoroughbreds (Mgr: P G Dunn)" dob="2015-08-17T00:00:00" age="2" sex="C" career="2-1-0-1 $42120.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="0-0-0-0" heavytrack="1-1-0-0 $33220.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-1-0-0 $33220.00" secondup="1-0-0-1 $8900.00" mindistancewin="0" maxdistancewin="0" finished="1" weightvariation="0" variedweight="58" decimalmargin="0.00" penalty="0" pricestarting="$3.60F" bonusindicator="E" />
<nomination number="6" saddlecloth="6" horse="Laburnum" id="5030242" idnumber="" regnumber="" blinkers="0" trainernumber="20602948" trainersurname="Cummings" trainerfirstname="James" trainertrack="Agnes Banks" rsbtrainername="James Cummings" jockeynumber="513320" jockeysurname="Schofield" jockeyfirstname="Glyn" barrier="4" weight="56" rating="0" description="B OR BR F 2 Denman x Runes (Redoute's Choice)" colours="Royal Blue" owners="Godolphin Australia Syndicate (Mgr: Vin Cox)" dob="2015-10-05T00:00:00" age="2" sex="F" career="4-1-1-1 $40705.00" thistrack="1-0-0-1 $8900.00" thisdistance="0-0-0-0" goodtrack="2-0-0-1 $10530.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-1-1-0 $30175.00" secondup="2-0-0-1 $10530.00" mindistancewin="0" maxdistancewin="0" finished="2" weightvariation="0" variedweight="56" decimalmargin="1.30" penalty="0" pricestarting="$3.70" bonusindicator="E" />
<nomination number="7" saddlecloth="7" horse="Aliferous" id="5027033" idnumber="" regnumber="" blinkers="0" trainernumber="17130" trainersurname="Lees" trainerfirstname="Kris" trainertrack="Newcastle" rsbtrainername="Kris Lees" jockeynumber="775359" jockeysurname="McDonald" jockeyfirstname="James" barrier="6" weight="55" rating="0" description="B F 2 Hinchinbrook x Willow (Woodman (USA))" colours="Royal Blue, White Horseshoe, Black And White Checked Sleeves And Cap" owners="Australian Bloodstock (L Murrell), Willow Racing (J Lovett) K Coughlan, N Whatmore, M French, M Haggarty, G Rigby, A Rhodes, C Sneddon, A Cammarano, D Girvan, G & M Barnett Racing, Maxim Racing No 2, J Lunn, D Love, M McLean, J Moffat, S Oliver & Pacific Boss" dob="2015-09-19T00:00:00" age="2" sex="F" career="1-1-0-0 $22255.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="1-1-0-0 $22255.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-1-0-0 $22255.00" secondup="0-0-0-0" mindistancewin="0" maxdistancewin="0" finished="3" weightvariation="0" variedweight="55" decimalmargin="1.50" penalty="0" pricestarting="$9" bonusindicator="E" />
<nomination number="4" saddlecloth="4" horse="Salsa Man" id="5027959" idnumber="" regnumber="" blinkers="1" trainernumber="11654" trainersurname="Perry" trainerfirstname="Paul" trainertrack="Newcastle" rsbtrainername="Paul Perry" jockeynumber="485880" jockeysurname="Ford" jockeyfirstname="Jay" barrier="3" weight="56.5" rating="0" description="B C 2 I Am Invincible x Ladies Dancing(NZ) (Zabeel (NZ))" colours="Light Blue, Lime Stripe And Halved Sleeves, Light Blue And Lime Spot Cap" owners="S N Gillard, Mrs L A Gillard, P M Perry & Ms C G Collis " dob="2015-10-01T00:00:00" age="2" sex="C" career="4-1-1-0 $31175.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="2-0-1-0 $8420.00" heavytrack="1-0-0-0 $500.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-0 $500.00" secondup="1-0-0-0 $500.00" mindistancewin="0" maxdistancewin="0" finished="4" weightvariation="0" variedweight="56.5" decimalmargin="1.90" penalty="0" pricestarting="$15" bonusindicator="E" />
<nomination number="8" saddlecloth="8" horse="Nobu (NZ)" id="5056474" idnumber="" regnumber="" blinkers="0" trainernumber="13964" trainersurname="Waller" trainerfirstname="Chris" trainertrack="Rosehill" rsbtrainername="Chris Waller" jockeynumber="25602" jockeysurname="McEvoy" jockeyfirstname="Kerrin" barrier="5" weight="55" rating="0" description="GR C 2 Reliable Man(GB) x Royal Prize(NZ) (Prized (USA))" colours="White, Navy Blue Stripes, Yellow Sleeves, Striped Cap" owners="M J Wyborn, Mrs C Wyborn & R R Peel-Walker" dob="2015-10-10T00:00:00" age="2" sex="C" career="2-0-0-0 $4950.00" thistrack="1-0-0-0 $4450.00" thisdistance="0-0-0-0" goodtrack="1-0-0-0 $4450.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-0 $500.00" secondup="1-0-0-0 $4450.00" mindistancewin="0" maxdistancewin="0" finished="5" weightvariation="0" variedweight="55" decimalmargin="3.70" penalty="0" pricestarting="$4.60" bonusindicator="" />
<nomination number="9" saddlecloth="9" horse="Neshmiya" id="5026836" idnumber="" regnumber="" blinkers="0" trainernumber="541922" trainersurname="" trainerfirstname="" trainertrack="Randwick" rsbtrainername="Peter & Paul Snowden" jockeynumber="866297" jockeysurname="Adkins" jockeyfirstname="Andrew" barrier="7" weight="54.5" rating="0" description="B F 2 Medaglia d'Oro(USA) x Al Anood (Danehill (USA))" colours="Yellow, Dark Blue Vee And Cap" owners="Sheikh Mohammed Bin Khalifa Al Maktoum" dob="2015-08-20T00:00:00" age="2" sex="F" career="2-0-1-0 $4365.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="0-0-0-0" heavytrack="1-0-0-0 $625.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-0 $625.00" secondup="1-0-1-0 $3740.00" mindistancewin="0" maxdistancewin="0" finished="6" weightvariation="0" variedweight="54.5" decimalmargin="6.00" penalty="0" pricestarting="$41" bonusindicator="E" />
<nomination number="2" saddlecloth="2" horse="Inherit" id="5029032" idnumber="" regnumber="" blinkers="0" trainernumber="13964" trainersurname="Waller" trainerfirstname="Chris" trainertrack="Rosehill" rsbtrainername="Chris Waller" jockeynumber="20944" jockeysurname="Bowman" jockeyfirstname="Hugh" barrier="2" weight="57" rating="0" description="B G 2 Pierro x In the Future (Redoute's Choice)" colours="Purple, White Stars And Cap" owners="Star Thoroughbreds Pty Ltd (Mgr: Ms D J Martin), M H Wood, Mrs M E Wood, F A Kennedy, Mrs B L Kennedy, Paramount Racing (Mgr: Ms K M Wood), T L Hastings, C G Olliver, R B T Aurisch & Mrs H A Aurisch" dob="2015-09-10T00:00:00" age="2" sex="G" career="1-1-0-0 $22255.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="1-1-0-0 $22255.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-1-0-0 $22255.00" secondup="0-0-0-0" mindistancewin="0" maxdistancewin="0" finished="7" weightvariation="0" variedweight="57" decimalmargin="7.10" penalty="0" pricestarting="$5.50" bonusindicator="E" />
<nomination number="3" saddlecloth="3" horse="Single Story" id="5028551" idnumber="" regnumber="" blinkers="1" trainernumber="541922" trainersurname="" trainerfirstname="" trainertrack="Randwick" rsbtrainername="Peter & Paul Snowden" jockeynumber="614120" jockeysurname="Clark" jockeyfirstname="Tim" barrier="2" weight="57" rating="0" description="B G 2 Not a Single Doubt x More Stories (More Than Ready (USA))" colours="Red, White Triple Crown Syndications Logo" owners="Triple Crown Syndications (Mgr: C H Ward), B I Toohey, C J Tracey, Mrs L J Chan, O K Chan, S A Norton, P W Brinkworth, R W Skillen, N W Woods, R Iponla, C N Ingham, Mrs E M Langworthy, D R Horn, Mrs K J Stevenson & A G Thomas & Emirates Park (Mgr: N A H Lootah) " dob="2015-09-25T00:00:00" age="2" sex="G" career="1-1-0-0 $22255.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="1-1-0-0 $22255.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-1-0-0 $22255.00" secondup="0-0-0-0" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="57" decimalmargin="0.00" penalty="0" pricestarting="" bonusindicator="E" />
<nomination number="5" saddlecloth="5" horse="Skyray" id="5031833" idnumber="" regnumber="" blinkers="0" trainernumber="10436" trainersurname="Conners" trainerfirstname="Clarry" trainertrack="Warwick Farm" rsbtrainername="Clarry Conners" jockeynumber="20643998" jockeysurname="Van Overmeire" jockeyfirstname="Jean" barrier="7" weight="56.5" rating="0" description="B G 2 Smart Missile x Distinctive (Falbrav (IRE))" colours="Yellow, Royal Blue Armbands And Cap" owners="Victory Lodge (C & M Conners) P J Collier, S P Sullivan, F J P Speechley, W R P Berens, S T Robinson, Mrs J L Robinson, J M Wood, R W Monteith, L W Kidd, A W Rohde, Mrs L M Rohde, Koll Racing (Mgr: K O'malley) & Jacobsen Bloodstock Pty Ltd (Mgr: P Jacobsen) " dob="2015-09-07T00:00:00" age="2" sex="G" career="8-1-3-0 $85805.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="5-1-1-0 $72355.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-0-2-0 $43390.00" secondup="2-0-1-0 $7790.00" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="56.5" decimalmargin="0.00" penalty="0" pricestarting="" bonusindicator="E" />
</race>
</meeting>
这是我尝试过的众多选择之一。
races = soup.findAll("race")
no_condition = list(
filter(lambda x: x == "condition", races))
print(no_condition)
还尝试了循环
for item in soup.findAll("race"):
if item.find("condition") not in item:
a.append(item)
continue
我想要的结果是
<race id="5159522" number="1" nomnumber="8" division="0" name="OWNERS RECOGNITION RACEDAY HANDICAP" mediumname="2Y HCP" shortname="2YO HCP" stage="Results" distance="1500" minweight="0.0" raisedweight="0.0" class="~" age="2" grade="0" weightcondition="HCP" trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="100000" first="53375" second="18800" third="8900" fourth="4450" fifth="2475" time="2018-07-28T11:45:00" bonustype="B001" nomsfee="0" acceptfee="0" trackcondition="Good" timingmethod="Electronic" fastesttime="1-32.17" sectionaltime="600/34.36" formavailable="0" racebookprize="Of $100000. First $53375, second $18800, third $8900, fourth $4450, fifth $2475, sixth $2000, seventh $2000, eighth $2000, ninth $2000, tenth $2000">
<nomination number="1" saddlecloth="1" horse="Thinkin' Big" id="5026846" idnumber="" regnumber="" blinkers="0" trainernumber="12083" trainersurname="" trainerfirstname="" trainertrack="Randwick" rsbtrainername="Gai Waterhouse & Adrian Bott" jockeynumber="541807" jockeysurname="Shinn" jockeyfirstname="Blake" barrier="1" weight="58" rating="0" description="B C 2 High Chaparral(IRE) x Nothin' Leica Cat(NZ) (Tale of the Cat (USA))" colours="White, Bottle Green Band And Sleeves, White And Bottle Green Quartered Cap, White Pom Pom" owners="Dodmark Thoroughbreds (Mgr: P G Dunn)" dob="2015-08-17T00:00:00" age="2" sex="C" career="2-1-0-1 $42120.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="0-0-0-0" heavytrack="1-1-0-0 $33220.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-1-0-0 $33220.00" secondup="1-0-0-1 $8900.00" mindistancewin="0" maxdistancewin="0" finished="1" weightvariation="0" variedweight="58" decimalmargin="0.00" penalty="0" pricestarting="$3.60F" bonusindicator="E" />
<nomination number="6" saddlecloth="6" horse="Laburnum" id="5030242" idnumber="" regnumber="" blinkers="0" trainernumber="20602948" trainersurname="Cummings" trainerfirstname="James" trainertrack="Agnes Banks" rsbtrainername="James Cummings" jockeynumber="513320" jockeysurname="Schofield" jockeyfirstname="Glyn" barrier="4" weight="56" rating="0" description="B OR BR F 2 Denman x Runes (Redoute's Choice)" colours="Royal Blue" owners="Godolphin Australia Syndicate (Mgr: Vin Cox)" dob="2015-10-05T00:00:00" age="2" sex="F" career="4-1-1-1 $40705.00" thistrack="1-0-0-1 $8900.00" thisdistance="0-0-0-0" goodtrack="2-0-0-1 $10530.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-1-1-0 $30175.00" secondup="2-0-0-1 $10530.00" mindistancewin="0" maxdistancewin="0" finished="2" weightvariation="0" variedweight="56" decimalmargin="1.30" penalty="0" pricestarting="$3.70" bonusindicator="E" />
<nomination number="7" saddlecloth="7" horse="Aliferous" id="5027033" idnumber="" regnumber="" blinkers="0" trainernumber="17130" trainersurname="Lees" trainerfirstname="Kris" trainertrack="Newcastle" rsbtrainername="Kris Lees" jockeynumber="775359" jockeysurname="McDonald" jockeyfirstname="James" barrier="6" weight="55" rating="0" description="B F 2 Hinchinbrook x Willow (Woodman (USA))" colours="Royal Blue, White Horseshoe, Black And White Checked Sleeves And Cap" owners="Australian Bloodstock (L Murrell), Willow Racing (J Lovett) K Coughlan, N Whatmore, M French, M Haggarty, G Rigby, A Rhodes, C Sneddon, A Cammarano, D Girvan, G & M Barnett Racing, Maxim Racing No 2, J Lunn, D Love, M McLean, J Moffat, S Oliver & Pacific Boss" dob="2015-09-19T00:00:00" age="2" sex="F" career="1-1-0-0 $22255.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="1-1-0-0 $22255.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-1-0-0 $22255.00" secondup="0-0-0-0" mindistancewin="0" maxdistancewin="0" finished="3" weightvariation="0" variedweight="55" decimalmargin="1.50" penalty="0" pricestarting="$9" bonusindicator="E" />
<nomination number="4" saddlecloth="4" horse="Salsa Man" id="5027959" idnumber="" regnumber="" blinkers="1" trainernumber="11654" trainersurname="Perry" trainerfirstname="Paul" trainertrack="Newcastle" rsbtrainername="Paul Perry" jockeynumber="485880" jockeysurname="Ford" jockeyfirstname="Jay" barrier="3" weight="56.5" rating="0" description="B C 2 I Am Invincible x Ladies Dancing(NZ) (Zabeel (NZ))" colours="Light Blue, Lime Stripe And Halved Sleeves, Light Blue And Lime Spot Cap" owners="S N Gillard, Mrs L A Gillard, P M Perry & Ms C G Collis " dob="2015-10-01T00:00:00" age="2" sex="C" career="4-1-1-0 $31175.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="2-0-1-0 $8420.00" heavytrack="1-0-0-0 $500.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-0 $500.00" secondup="1-0-0-0 $500.00" mindistancewin="0" maxdistancewin="0" finished="4" weightvariation="0" variedweight="56.5" decimalmargin="1.90" penalty="0" pricestarting="$15" bonusindicator="E" />
<nomination number="8" saddlecloth="8" horse="Nobu (NZ)" id="5056474" idnumber="" regnumber="" blinkers="0" trainernumber="13964" trainersurname="Waller" trainerfirstname="Chris" trainertrack="Rosehill" rsbtrainername="Chris Waller" jockeynumber="25602" jockeysurname="McEvoy" jockeyfirstname="Kerrin" barrier="5" weight="55" rating="0" description="GR C 2 Reliable Man(GB) x Royal Prize(NZ) (Prized (USA))" colours="White, Navy Blue Stripes, Yellow Sleeves, Striped Cap" owners="M J Wyborn, Mrs C Wyborn & R R Peel-Walker" dob="2015-10-10T00:00:00" age="2" sex="C" career="2-0-0-0 $4950.00" thistrack="1-0-0-0 $4450.00" thisdistance="0-0-0-0" goodtrack="1-0-0-0 $4450.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-0 $500.00" secondup="1-0-0-0 $4450.00" mindistancewin="0" maxdistancewin="0" finished="5" weightvariation="0" variedweight="55" decimalmargin="3.70" penalty="0" pricestarting="$4.60" bonusindicator="" />
<nomination number="9" saddlecloth="9" horse="Neshmiya" id="5026836" idnumber="" regnumber="" blinkers="0" trainernumber="541922" trainersurname="" trainerfirstname="" trainertrack="Randwick" rsbtrainername="Peter & Paul Snowden" jockeynumber="866297" jockeysurname="Adkins" jockeyfirstname="Andrew" barrier="7" weight="54.5" rating="0" description="B F 2 Medaglia d'Oro(USA) x Al Anood (Danehill (USA))" colours="Yellow, Dark Blue Vee And Cap" owners="Sheikh Mohammed Bin Khalifa Al Maktoum" dob="2015-08-20T00:00:00" age="2" sex="F" career="2-0-1-0 $4365.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="0-0-0-0" heavytrack="1-0-0-0 $625.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-0 $625.00" secondup="1-0-1-0 $3740.00" mindistancewin="0" maxdistancewin="0" finished="6" weightvariation="0" variedweight="54.5" decimalmargin="6.00" penalty="0" pricestarting="$41" bonusindicator="E" />
<nomination number="2" saddlecloth="2" horse="Inherit" id="5029032" idnumber="" regnumber="" blinkers="0" trainernumber="13964" trainersurname="Waller" trainerfirstname="Chris" trainertrack="Rosehill" rsbtrainername="Chris Waller" jockeynumber="20944" jockeysurname="Bowman" jockeyfirstname="Hugh" barrier="2" weight="57" rating="0" description="B G 2 Pierro x In the Future (Redoute's Choice)" colours="Purple, White Stars And Cap" owners="Star Thoroughbreds Pty Ltd (Mgr: Ms D J Martin), M H Wood, Mrs M E Wood, F A Kennedy, Mrs B L Kennedy, Paramount Racing (Mgr: Ms K M Wood), T L Hastings, C G Olliver, R B T Aurisch & Mrs H A Aurisch" dob="2015-09-10T00:00:00" age="2" sex="G" career="1-1-0-0 $22255.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="1-1-0-0 $22255.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-1-0-0 $22255.00" secondup="0-0-0-0" mindistancewin="0" maxdistancewin="0" finished="7" weightvariation="0" variedweight="57" decimalmargin="7.10" penalty="0" pricestarting="$5.50" bonusindicator="E" />
<nomination number="3" saddlecloth="3" horse="Single Story" id="5028551" idnumber="" regnumber="" blinkers="1" trainernumber="541922" trainersurname="" trainerfirstname="" trainertrack="Randwick" rsbtrainername="Peter & Paul Snowden" jockeynumber="614120" jockeysurname="Clark" jockeyfirstname="Tim" barrier="2" weight="57" rating="0" description="B G 2 Not a Single Doubt x More Stories (More Than Ready (USA))" colours="Red, White Triple Crown Syndications Logo" owners="Triple Crown Syndications (Mgr: C H Ward), B I Toohey, C J Tracey, Mrs L J Chan, O K Chan, S A Norton, P W Brinkworth, R W Skillen, N W Woods, R Iponla, C N Ingham, Mrs E M Langworthy, D R Horn, Mrs K J Stevenson & A G Thomas & Emirates Park (Mgr: N A H Lootah) " dob="2015-09-25T00:00:00" age="2" sex="G" career="1-1-0-0 $22255.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="1-1-0-0 $22255.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-1-0-0 $22255.00" secondup="0-0-0-0" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="57" decimalmargin="0.00" penalty="0" pricestarting="" bonusindicator="E" />
<nomination number="5" saddlecloth="5" horse="Skyray" id="5031833" idnumber="" regnumber="" blinkers="0" trainernumber="10436" trainersurname="Conners" trainerfirstname="Clarry" trainertrack="Warwick Farm" rsbtrainername="Clarry Conners" jockeynumber="20643998" jockeysurname="Van Overmeire" jockeyfirstname="Jean" barrier="7" weight="56.5" rating="0" description="B G 2 Smart Missile x Distinctive (Falbrav (IRE))" colours="Yellow, Royal Blue Armbands And Cap" owners="Victory Lodge (C & M Conners) P J Collier, S P Sullivan, F J P Speechley, W R P Berens, S T Robinson, Mrs J L Robinson, J M Wood, R W Monteith, L W Kidd, A W Rohde, Mrs L M Rohde, Koll Racing (Mgr: K O'malley) & Jacobsen Bloodstock Pty Ltd (Mgr: P Jacobsen) " dob="2015-09-07T00:00:00" age="2" sex="G" career="8-1-3-0 $85805.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="5-1-1-0 $72355.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-0-2-0 $43390.00" secondup="2-0-1-0 $7790.00" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="56.5" decimalmargin="0.00" penalty="0" pricestarting="" bonusindicator="E" />
</race>
答案 0 :(得分:1)
如果我正确理解,则想删除所有<condition>
标签。您可以使用.extract()
[bs4-doc]:
for cond in soup.find_all('condition'):
cond.extract()
在此循环之后,<condition>
的所有soup
标签将消失,print(..)
soup
的{{1}}将导致没有这些标签的流。
话虽这么说,通常可以使用XSLT完成简单的XML处理,XSLT是XML中的XML转换标准...)。
例如:
>>> soup
<?xml version="1.0" encoding="utf-8"?>
<meeting>
<race acceptfee="0" age="2" bonustype="B001" class="~" distance="1500" division="0" fastesttime="1-32.17" fifth="2475" first="53375" formavailable="0" fourth="4450" grade="0" id="5159522" jockey="0" mediumname="2Y HCP" minweight="0.0" name="OWNERS RECOGNITION RACEDAY HANDICAP" nomnumber="8" nomsfee="0" number="1" owner="0" racebookprize="Of $100000. First $53375, second $18800, third $8900, fourth $4450, fifth $2475, sixth $2000, seventh $2000, eighth $2000, ninth $2000, tenth $2000" raisedweight="0.0" second="18800" sectionaltime="600/34.36" shortname="2YO HCP" stage="Results" strapper="0" third="8900" time="2018-07-28T11:45:00" timingmethod="Electronic" totalprize="100000" trackcondition="Good" trainer="0" trophy="0" weightcondition="HCP">
<condition line="1">Of $100000. First $53375, second $18800, third $8900, fourth $4450, fifth $2475, sixth $2000, seventh $2000, eighth $2000, ninth $2000, tenth $2000</condition>
<condition line="3">No class restriction, Handicap, For Two-Years-Old, No sex restriction</condition>
<nomination age="2" barrier="1" blinkers="0" bonusindicator="E" career="2-1-0-1 $42120.00" colours="White, Bottle Green Band And Sleeves, White And Bottle Green Quartered Cap, White Pom Pom" deadtrack="" decimalmargin="0.00" description="B C 2 High Chaparral(IRE) x Nothin' Leica Cat(NZ) (Tale of the Cat (USA))" dob="2015-08-17T00:00:00" fasttrack="0-0-0-0" finished="1" firstup="1-1-0-0 $33220.00" goodtrack="0-0-0-0" heavytrack="1-1-0-0 $33220.00" horse="Thinkin' Big" id="5026846" idnumber="" jockeyfirstname="Blake" jockeynumber="541807" jockeysurname="Shinn" maxdistancewin="0" mindistancewin="0" number="1" owners="Dodmark Thoroughbreds (Mgr: P G Dunn)" penalty="0" pricestarting="$3.60F" rating="0" regnumber="" rsbtrainername="Gai Waterhouse & Adrian Bott" saddlecloth="1" secondup="1-0-0-1 $8900.00" sex="C" slowtrack="" thisdistance="0-0-0-0" thistrack="0-0-0-0" trainerfirstname="" trainernumber="12083" trainersurname="" trainertrack="Randwick" variedweight="58" weight="58" weightvariation="0"/>
<nomination age="2" barrier="4" blinkers="0" bonusindicator="E" career="4-1-1-1 $40705.00" colours="Royal Blue" deadtrack="" decimalmargin="1.30" description="B OR BR F 2 Denman x Runes (Redoute's Choice)" dob="2015-10-05T00:00:00" fasttrack="0-0-0-0" finished="2" firstup="2-1-1-0 $30175.00" goodtrack="2-0-0-1 $10530.00" heavytrack="0-0-0-0" horse="Laburnum" id="5030242" idnumber="" jockeyfirstname="Glyn" jockeynumber="513320" jockeysurname="Schofield" maxdistancewin="0" mindistancewin="0" number="6" owners="Godolphin Australia Syndicate (Mgr: Vin Cox)" penalty="0" pricestarting="$3.70" rating="0" regnumber="" rsbtrainername="James Cummings" saddlecloth="6" secondup="2-0-0-1 $10530.00" sex="F" slowtrack="" thisdistance="0-0-0-0" thistrack="1-0-0-1 $8900.00" trainerfirstname="James" trainernumber="20602948" trainersurname="Cummings" trainertrack="Agnes Banks" variedweight="56" weight="56" weightvariation="0"/>
</race>
</meeting>
>>> for cond in soup.find_all('condition'):
... cond.extract()
...
<condition line="1">Of $100000. First $53375, second $18800, third $8900, fourth $4450, fifth $2475, sixth $2000, seventh $2000, eighth $2000, ninth $2000, tenth $2000</condition>
<condition line="3">No class restriction, Handicap, For Two-Years-Old, No sex restriction</condition>
>>> soup
<?xml version="1.0" encoding="utf-8"?>
<meeting>
<race acceptfee="0" age="2" bonustype="B001" class="~" distance="1500" division="0" fastesttime="1-32.17" fifth="2475" first="53375" formavailable="0" fourth="4450" grade="0" id="5159522" jockey="0" mediumname="2Y HCP" minweight="0.0" name="OWNERS RECOGNITION RACEDAY HANDICAP" nomnumber="8" nomsfee="0" number="1" owner="0" racebookprize="Of $100000. First $53375, second $18800, third $8900, fourth $4450, fifth $2475, sixth $2000, seventh $2000, eighth $2000, ninth $2000, tenth $2000" raisedweight="0.0" second="18800" sectionaltime="600/34.36" shortname="2YO HCP" stage="Results" strapper="0" third="8900" time="2018-07-28T11:45:00" timingmethod="Electronic" totalprize="100000" trackcondition="Good" trainer="0" trophy="0" weightcondition="HCP">
<nomination age="2" barrier="1" blinkers="0" bonusindicator="E" career="2-1-0-1 $42120.00" colours="White, Bottle Green Band And Sleeves, White And Bottle Green Quartered Cap, White Pom Pom" deadtrack="" decimalmargin="0.00" description="B C 2 High Chaparral(IRE) x Nothin' Leica Cat(NZ) (Tale of the Cat (USA))" dob="2015-08-17T00:00:00" fasttrack="0-0-0-0" finished="1" firstup="1-1-0-0 $33220.00" goodtrack="0-0-0-0" heavytrack="1-1-0-0 $33220.00" horse="Thinkin' Big" id="5026846" idnumber="" jockeyfirstname="Blake" jockeynumber="541807" jockeysurname="Shinn" maxdistancewin="0" mindistancewin="0" number="1" owners="Dodmark Thoroughbreds (Mgr: P G Dunn)" penalty="0" pricestarting="$3.60F" rating="0" regnumber="" rsbtrainername="Gai Waterhouse & Adrian Bott" saddlecloth="1" secondup="1-0-0-1 $8900.00" sex="C" slowtrack="" thisdistance="0-0-0-0" thistrack="0-0-0-0" trainerfirstname="" trainernumber="12083" trainersurname="" trainertrack="Randwick" variedweight="58" weight="58" weightvariation="0"/>
<nomination age="2" barrier="4" blinkers="0" bonusindicator="E" career="4-1-1-1 $40705.00" colours="Royal Blue" deadtrack="" decimalmargin="1.30" description="B OR BR F 2 Denman x Runes (Redoute's Choice)" dob="2015-10-05T00:00:00" fasttrack="0-0-0-0" finished="2" firstup="2-1-1-0 $30175.00" goodtrack="2-0-0-1 $10530.00" heavytrack="0-0-0-0" horse="Laburnum" id="5030242" idnumber="" jockeyfirstname="Glyn" jockeynumber="513320" jockeysurname="Schofield" maxdistancewin="0" mindistancewin="0" number="6" owners="Godolphin Australia Syndicate (Mgr: Vin Cox)" penalty="0" pricestarting="$3.70" rating="0" regnumber="" rsbtrainername="James Cummings" saddlecloth="6" secondup="2-0-0-1 $10530.00" sex="F" slowtrack="" thisdistance="0-0-0-0" thistrack="1-0-0-1 $8900.00" trainerfirstname="James" trainernumber="20602948" trainersurname="Cummings" trainertrack="Agnes Banks" variedweight="56" weight="56" weightvariation="0"/>
</race>
</meeting>
因此,在这里我们看到cond.extract()
返回XML元素((这些元素打印在for
循环中,但是我们对此不做任何操作,在Python程序中,值当然是未打印)。在for
循环之后,所有<condition>
标签都将被删除。