Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQuery How do I append and rename an element into each of its subelements

Tags:

How can I append or insert a class attribute into its sub-elements, but only for direct children and then to repeat it for the next class and sub-elements.

In the docs it is referenced here pyquery manipulating

>>> d = pq('<html><body><div id="test"><a href="http://python.org">python</a> !</div></body></html>') >>> p.prependTo(d('#test')) [<p#hello.hello>] >>> d('#test').html() u'<p class="hello" ..

Sample

<meeting id="42499" barriertrial="0" venue="Royal Randwick" date="2016-04-09T00:00:00" gearchanges="-1" stewardsreport="-1" gearlist="-1" racebook="0" postracestewards="0" meetingtype="TAB" rail="Timing - Electronic : Rail - +6m 1600-wp +3m Rem" weather="Fine      " trackcondition="Soft 6    " nomsdeadline="2016-04-04T11:00:00" weightsdeadline="2016-04-05T16:00:00" acceptdeadline="2016-04-06T09:00:00" jockeydeadline="2016-04-06T12:00:00">
  <club abbrevname="Australian Turf Club" code="56398" associationclass="1" website="http://" />
  <race id="211911" number="1" nomnumber="10" division="0" name="TFE HOTELS FERNHILL HANDICAP" mediumname="FERNHILL" shortname="FERNHILL HCP" stage="Results" distance="1600" minweight="0" raisedweight="0" class="~         " age="2         " grade="0" weightcondition="QLT       " trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="150000" first="90000" second="30000" third="15000" fourth="7500" fifth="3000" time="2016-04-09T11:30:00" bonustype="          " nomsfee="0" acceptfee="0" trackcondition="Soft 6    " timingmethod="Electronic" fastesttime="1-37.74   " sectionaltime="600/36.43 " formavailable="0" racebookprize="Of $150000. First $90000, second $30000, third $15000, fourth $7500, fifth $3000, sixth $1500, seventh $1500, eighth $1500">
    <condition line="1">Of $150000. First $90000, second $30000, third $15000, fourth $7500, fifth $3000, sixth $1500, seventh $1500, eighth $1500</condition>
    <condition line="3">No class restriction, Quality, For Two-Years-Old, No sex restriction, (Listed)</condition>
    <condition line="5">No Allowances for apprentices. Field Limit: 16 + 4 EM</condition>
    <condition line="10">(LISTED RACE) : MINIMUM WEIGHT 53KG : MAXIMUM WEIGHT 61KG : NO APPRENTICE CLAIMS</condition>
    <nomination number="1" saddlecloth="1" horse="Prized Icon" id="198206" idnumber="" regnumber="" blinkers="1" trainernumber="81483" trainersurname="Cummings" trainerfirstname="James" trainertrack="Randwick" rsbtrainername="James Cummings" jockeynumber="349" jockeysurname="Bowman" jockeyfirstname="Hugh" barrier="7" weight="58" rating="0" description="BR C 2 More Than Ready(USA) x Tropical Affair (Danehill(USA))" colours="Red, Black Stripes" owners="Gooree Stud (E M Cojuangco &amp; M C Cummings)" dob="2013-09-02T00:00:00" age="3" sex="C" career="5-0-2-3 $105850.00" thistrack="3-0-1-2 $48350.00" thisdistance="0-0-0-0" goodtrack="4-0-2-2 $97500.00" heavytrack="1-0-0-1 $8350.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-0-0-2 $23350.00" secondup="1-0-1-0 $25000.00" mindistancewin="0" maxdistancewin="0" finished="1" weightvariation="0" variedweight="58" decimalmargin="0.00" penalty="0" pricestarting="$2.80F" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="4" saddlecloth="4" horse="Crown Him" id="202278" idnumber="" regnumber="" blinkers="0" trainernumber="38701" trainersurname="Cummings" trainerfirstname="Anthony" trainertrack="Randwick" rsbtrainername="Anthony Cummings" jockeynumber="86596" jockeysurname="Collett" jockeyfirstname="Jason" barrier="4" weight="55" rating="0" description="BR G 2 Casino Prince x Ambitious Princess(NZ) (Savabeel)" colours="Grey, Red Sleeves, White Armbands And Cap" owners="P D L Horwitz, Stags(A J Falk), P Davidson, S Smith, G Macdonald, P Colley, S Horwitz, H De Jong, Mrs H F Horwitz, Ms B G Horwitz &amp; J D L Horwitz" dob="2013-09-16T00:00:00" age="3" sex="G" career="2-0-0-0 $7950.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="2-0-0-0 $7950.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-0 $450.00" secondup="1-0-0-0 $7500.00" mindistancewin="0" maxdistancewin="0" finished="2" weightvariation="0" variedweight="55" decimalmargin="2.00" penalty="0" pricestarting="$4.20" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="7" saddlecloth="7" horse="Lasqueti Spirit" id="198197" idnumber="" regnumber="" blinkers="1" trainernumber="803" trainersurname="Curtis" trainerfirstname="Lee" trainertrack="Rosehill" rsbtrainername="Lee Curtis" jockeynumber="86924" jockeysurname="Costin" jockeyfirstname="Winona" barrier="1" weight="54" rating="0" description="B F 2 Beneteau x Supriym Story (General Nediym)" colours="Maroon, White Crossed Sashes, Yellow Armbands And Cap" owners="Mystery Downs (Mgr: F W Cook), Mrs C M Cook, A R Davison, Ms C E Curtis, R Croad, B Djura &amp; M J Fenech" dob="2013-10-09T00:00:00" age="3" sex="F" career="2-0-0-1 $2100.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="2-0-0-1 $2100.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-1 $2100.00" secondup="1-0-0-0" mindistancewin="0" maxdistancewin="0" finished="3" weightvariation="0" variedweight="54" decimalmargin="2.20" penalty="0" pricestarting="$41.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="6" saddlecloth="6" horse="Bring Me the Bling" id="200603" idnumber="" regnumber="" blinkers="0" trainernumber="77974" trainersurname="Hawkes" trainerfirstname="Michael" trainertrack="Rosehill" rsbtrainername="Michael, Wayne &amp; John Hawkes" jockeynumber="51661" jockeysurname="Berry" jockeyfirstname="Tommy" barrier="2" weight="54" rating="0" description="CH F 2 Sebring x Anuket (Nuclear Freeze(USA))" colours="Orange" owners="Mrs C A Hawkes, Mrs J E Hawkes, Mrs V Francis, Mrs S L Fownes, Mrs F Fowles, Mrs J A Baldacchino, Mrs S Russo, Mrs G Koundouris, Mrs L Cox, Mrs A M Orton, Mrs S Smith, Mrs A M Duggan &amp; Mrs C Briggs" dob="2013-08-15T00:00:00" age="3" sex="F" career="3-0-0-1 $7475.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="3-0-0-1 $7475.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-0-0-1 $7100.00" secondup="1-0-0-0 $375.00" mindistancewin="0" maxdistancewin="0" finished="4" weightvariation="0" variedweight="54" decimalmargin="2.40" penalty="0" pricestarting="$21.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="2" saddlecloth="2" horse="Reinforced" id="201692" idnumber="" regnumber="" blinkers="1" trainernumber="81215" trainersurname="" trainerfirstname="" trainertrack="Randwick" rsbtrainername="Peter &amp; Paul Snowden" jockeynumber="40133" jockeysurname="Shinn" jockeyfirstname="Blake" barrier="3" weight="55.5" rating="0" description="BR OR BLK C 2 Snitzel x Shower of Steel (Comet Shine(CAN))" colours="Dark Green, Gold Epaulettes" owners="James Harron Bloodstock, Newgate S F, Horse Ventures, EST E G T Bateman, Mrs B C Bateman, Doyles Breeding &amp; Racing, Rockingham Thoroughbreds, Mr J A  &amp; Mrs F A Ingham, Love Racing, G1G Racing &amp; Breeding, Mr P M Ballard, Mr D Saab, A P B International &amp; Mrs J M Ballard" dob="2013-08-18T00:00:00" age="3" sex="C" career="4-1-1-1 $23600.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="3-1-1-0 $21500.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-1 $2100.00" secondup="1-0-1-0 $4250.00" mindistancewin="0" maxdistancewin="0" finished="5" weightvariation="0" variedweight="55.5" decimalmargin="2.80" penalty="0" pricestarting="$3.20" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="8" saddlecloth="8" horse="Gravitate" id="198221" idnumber="" regnumber="" blinkers="1" trainernumber="81215" trainersurname="" trainerfirstname="" trainertrack="Randwick" rsbtrainername="Peter &amp; Paul Snowden" jockeynumber="1472" jockeysurname="McEvoy" jockeyfirstname="Kerrin" barrier="5" weight="54" rating="0" description="B C 2 Sebring x Forward Love (Charge Forward)" colours="Dark Green, Gold Epaulettes" owners="James Harron Bloodstock, Newgate S F, Horse Ventures, EST E G T Bateman, Mrs B C Bateman, Doyles Breeding &amp; Racing, Rockingham Thoroughbreds, J A &amp; Mrs F A Ingham, Love Racing, B L Leonard &amp; Mrs D B Leonard" dob="2013-09-23T00:00:00" age="3" sex="C" career="2-0-0-0 $1405.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="1-0-0-0 $955.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-0 $450.00" secondup="1-0-0-0 $955.00" mindistancewin="0" maxdistancewin="0" finished="6" weightvariation="0" variedweight="54" decimalmargin="15.50" penalty="0" pricestarting="$12.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="3" saddlecloth="3" horse="Hutcho" id="200622" idnumber="" regnumber="" blinkers="1" trainernumber="321" trainersurname="Portelli" trainerfirstname="Gary" trainertrack="Warwick Farm" rsbtrainername="Gary Portelli" jockeynumber="86721" jockeysurname="Dunn" jockeyfirstname="Dwayne" barrier="6" weight="55" rating="0" description="B OR BR C 2 Congrats(USA) x Kylie (Danehill Dancer(IRE))" colours="Red, White Champion Thoroughbreds Logo And Sleeves, Quartered Cap" owners="Ronstel Thoroughbreds (Mgr: R H Burns)Champion Thoroughbreds (Mgr: J P Abrahams)Sommita (Mgr: R J Puiatti) Sedgewyn (Mgr: J E Abrahams) Racovalis, Mrs V Racovalis, P Carswell, M Bhatia, D W Willis, Mrs J S Willis, S A Miller, Mrs L Miller, Mrs R Norton, G Seto &amp; Mrs A Gibson" dob="2013-09-11T00:00:00" age="3" sex="C" career="3-0-0-0 $14750.00" thistrack="1-0-0-0 $1000.00" thisdistance="0-0-0-0" goodtrack="2-0-0-0 $13750.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-0-0-0 $14750.00" secondup="1-0-0-0" mindistancewin="0" maxdistancewin="0" finished="7" weightvariation="0" variedweight="55" decimalmargin="26.60" penalty="0" pricestarting="$18.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="5" saddlecloth="5" horse="Obscura" id="200142" idnumber="" regnumber="" blinkers="1" trainernumber="235" trainersurname="O'Shea" trainerfirstname="John" trainertrack="Warwick Farm" rsbtrainername="John O'Shea" jockeynumber="86876" jockeysurname="McDonald" jockeyfirstname="James" barrier="8" weight="55" rating="0" description="B C 2 Street Cry(IRE) x Conceal (Redoute's Choice)" colours="Royal Blue" owners="Godolphin" dob="2013-11-15T00:00:00" age="3" sex="C" career="2-0-0-1 $5100.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="2-0-0-1 $5100.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-1 $2100.00" secondup="1-0-0-0 $3000.00" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="55" decimalmargin="0.00" penalty="0" pricestarting="NA" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
  </race>
  <race id="211912" number="2" nomnumber="9" division="0" name="MCGRATH ESTATE AGENTS SOUTH PACIFIC CLASSIC" mediumname="S/PACIFIC" shortname="SOUTH PACIFIC" stage="Results" distance="1400" minweight="0" raisedweight="0" class="~         " age="3         " grade="0" weightcondition="QLT       " trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="150000" first="90000" second="30000" third="15000" fourth="7500" fifth="3000" time="2016-04-09T12:10:00" bonustype="          " nomsfee="0" acceptfee="0" trackcondition="Soft 6    " timingmethod="Electronic" fastesttime="1-22.70   " sectionaltime="600/35.57 " formavailable="0" racebookprize="Of $150000. First $90000, second $30000, third $15000, fourth $7500, fifth $3000, sixth $1500, seventh $1500, eighth $1500">
    <condition line="1">Of $150000. First $90000, second $30000, third $15000, fourth $7500, fifth $3000, sixth $1500, seventh $1500, eighth $1500</condition>
    <condition line="3">No class restriction, Quality, For Three-Years-Old, No sex restriction, (Listed)</condition>
    <condition line="5">No Allowances for apprentices. Field Limit: 15 + 4 EM</condition>
    <condition line="10">(LISTED RACE) : MINIMUM WEIGHT 53KG : MAXIMUM WEIGHT 61KG : NO APPRENTICE CLAIMS</condition>
    <nomination number="8" saddlecloth="8" horse="Handfast" id="187878" idnumber="" regnumber="" blinkers="1" trainernumber="235" trainersurname="O'Shea" trainerfirstname="John" trainertrack="Warwick Farm" rsbtrainername="John O'Shea" jockeynumber="86876" jockeysurname="McDonald" jockeyfirstname="James" barrier="1" weight="55.5" rating="75" description="CH G 3 Street Cry(IRE) x Lobola (Anabaa(USA))" colours="Royal Blue" owners="Godolphin" dob="2012-10-05T00:00:00" age="4" sex="G" career="6-2-2-0 $92685.00" thistrack="0-0-0-0" thisdistance="2-1-1-0 $53025.00" goodtrack="4-2-2-0 $90275.00" heavytrack="1-0-0-0 $525.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-1-0-0 $17775.00" secondup="2-1-1-0 $53025.00" mindistancewin="0" maxdistancewin="0" finished="1" weightvariation="0" variedweight="55.5" decimalmargin="0.00" penalty="0" pricestarting="$9.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="1" saddlecloth="1" horse="Santa Ana Lane" id="198535" idnumber="" regnumber="" blinkers="0" trainernumber="81314" trainersurname="" trainerfirstname="" trainertrack="Flemington" rsbtrainername="Lee &amp; Anthony Freedman" jockeynumber="349" jockeysurname="Bowman" jockeyfirstname="Hugh" barrier="9" weight="59" rating="82" description="B G 3 Lope de Vega(IRE) x Fast Fleet (Fastnet Rock)" colours="Royal Blue, Gold Armbands And Cap" owners="William St (Mgr: M A Ramsden)" dob="2012-09-11T00:00:00" age="4" sex="G" career="5-3-0-0 $130450.00" thistrack="0-0-0-0" thisdistance="1-0-0-0 $5400.00" goodtrack="4-2-0-0 $115800.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-2-0-0 $112400.00" secondup="2-1-0-0 $12650.00" mindistancewin="0" maxdistancewin="0" finished="2" weightvariation="0" variedweight="59" decimalmargin="1.80" penalty="0" pricestarting="$10.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="6" saddlecloth="6" horse="Serene Majesty" id="187868" idnumber="" regnumber="" blinkers="1" trainernumber="81215" trainersurname="" trainerfirstname="" trainertrack="Randwick" rsbtrainername="Peter &amp; Paul Snowden" jockeynumber="1472" jockeysurname="McEvoy" jockeyfirstname="Kerrin" barrier="4" weight="57.5" rating="83" description="B F 3 Fastnet Rock x Custard (Lonhro)" colours="White, Old Gold Stars And Sleeves, Old Gold Cap With White Pom Pom" owners="Werrett Bloodstock Pty Ltd (Mgr: N R J Werrett), G J Wilkie, Mrs K J Wilkie, Mrs P A Hawkes, B R Hawkes, C H Madden, Mrs J G Madden, Dr E K Attebo, Dr A K Cree, Dr A L Attebo, K A G Attebo, Mrs J V Allan, A D Allan, B P Bradley &amp;  Mrs A L Bradley " dob="2012-09-09T00:00:00" age="4" sex="F" career="7-2-1-1 $145500.00" thistrack="2-0-0-0 $3000.00" thisdistance="0-0-0-0" goodtrack="5-2-0-1 $135825.00" heavytrack="1-0-1-0 $7925.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="3-1-1-0 $42200.00" secondup="2-0-0-0 $1750.00" mindistancewin="0" maxdistancewin="0" finished="3" weightvariation="0" variedweight="57.5" decimalmargin="5.10" penalty="0" pricestarting="$31.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="11" saddlecloth="11" horse="Craiglea Wandoo" id="188923" idnumber="" regnumber="" blinkers="1" trainernumber="81191" trainersurname="Johnston" trainerfirstname="Krystle" trainertrack="Sunshine Coast" rsbtrainername="Krystle Johnston" jockeynumber="55044" jockeysurname="King" jockeyfirstname="Paul" barrier="5" weight="54" rating="72" description="B G 3 Warhead x Craiglea Symphony (Waterford Road)" colours="Royal Blue, White Crossed Sashes And Striped Sleeves" owners="S M Johnston, Mrs M L Johnston, W A Johnston, Miss K J E Johnston, J J Johnston &amp; J J Johnston    " dob="2012-10-22T00:00:00" age="4" sex="G" career="26-10-3-2 $286500.00" thistrack="0-0-0-0" thisdistance="4-0-0-1 $12400.00" goodtrack="18-7-3-1 $220950.00" heavytrack="2-1-0-0 $16950.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-0-1-1 $14300.00" secondup="2-0-0-0 $3100.00" mindistancewin="0" maxdistancewin="0" finished="4" weightvariation="0" variedweight="54" decimalmargin="5.20" penalty="0" pricestarting="$71.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="10" saddlecloth="10" horse="Chandana" id="198912" idnumber="" regnumber="" blinkers="0" trainernumber="681" trainersurname="Waller" trainerfirstname="Chris" trainertrack="Rosehill" rsbtrainername="Chris Waller" jockeynumber="84015" jockeysurname="Avdulla" jockeyfirstname="Brenton" barrier="14" weight="54" rating="72" description="B F 3 High Chaparral(IRE) x Bombay (Encosta de Lago)" colours="Red, White Stripe, Royal Blue Sleeves And Cap, White Spot, Red Peak" owners="B L Mathieson, R Smith, I Bruce, P Ng, F W Cook, Mrs C M Cook, K H Warriner, J T C Lake, D M Ackery, Mrs K Ackery, R Shrimpton &amp; Mrs C Shrimpton, Magic Bloodstock Racing (Mgr: P G Tighe)" dob="2012-09-11T00:00:00" age="4" sex="F" career="5-2-0-3 $90950.00" thistrack="1-1-0-0 $39125.00" thisdistance="2-1-0-1 $41375.00" goodtrack="3-1-0-2 $43475.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-0-0-1 $2100.00" secondup="1-1-0-0 $33025.00" mindistancewin="0" maxdistancewin="0" finished="5" weightvariation="0" variedweight="54" decimalmargin="5.30" penalty="0" pricestarting="$8.50" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="7" saddlecloth="7" horse="King's Troop" id="191860" idnumber="" regnumber="" blinkers="0" trainernumber="685" trainersurname="Ryan" trainerfirstname="Gerald" trainertrack="Rosehill" rsbtrainername="Gerald Ryan" jockeynumber="40275" jockeysurname="Clark" jockeyfirstname="Tim" barrier="11" weight="56" rating="76" description="B C 3 Redoute's Choice x Royal Snippets (Royal Academy(USA))" colours="Black And Yellow Diamonds, Yellow Cap" owners="Arrowfield Pastoral Pty Ltd &amp; Belford Productions Pty Ltd" dob="2012-08-30T00:00:00" age="4" sex="C" career="7-1-2-0 $85425.00" thistrack="2-0-0-0 $4150.00" thisdistance="0-0-0-0" goodtrack="5-1-1-0 $64525.00" heavytrack="1-0-1-0 $16750.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-0-1-0 $20900.00" secondup="2-1-0-0 $38025.00" mindistancewin="0" maxdistancewin="0" finished="6" weightvariation="0" variedweight="56" decimalmargin="5.40" penalty="0" pricestarting="$26.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="5" saddlecloth="5" horse="Suspense" id="193350" idnumber="" regnumber="" blinkers="0" trainernumber="81215" trainersurname="" trainerfirstname="" trainertrack="Randwick" rsbtrainername="Peter &amp; Paul Snowden" jockeynumber="884" jockeysurname="Zahra" jockeyfirstname="Mark" barrier="3" weight="58" rating="80" description="B G 3 Keano(NZ) x Intrigues (Night Shift(USA))" colours="Red, White Seams, Checked Sleeves And Cap" owners="Mrs L S Snowden, Mrs L J Harvey, D A Harvey, Mrs K Snowden, J F Purcell, Mrs M F Purcell, J T Vassallo, Mrs L J Vassallo, V Santoro &amp; Ms S J Philcox" dob="2012-11-25T00:00:00" age="4" sex="G" career="4-4-0-0 $87425.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="3-3-0-0 $75175.00" heavytrack="1-1-0-0 $12250.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-2-0-0 $35275.00" secondup="1-1-0-0 $23025.00" mindistancewin="0" maxdistancewin="0" finished="7" weightvariation="0" variedweight="58" decimalmargin="6.10" penalty="0" pricestarting="$9.50" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="4" saddlecloth="4" horse="Look to the Stars" id="187866" idnumber="" regnumber="" blinkers="0" trainernumber="78212" trainersurname="Vandyke" trainerfirstname="David" trainertrack="Warwick Farm" rsbtrainername="David Vandyke" jockeynumber="57545" jockeysurname="Spriggs" jockeyfirstname="Blake" barrier="12" weight="58" rating="84" description="B F 3 I Am Invincible x Star Prize(ARG) (Southern Halo(USA))" colours="White, Darby Racing Logo, Navy Blue Armbands With Darby Racing Logo, Navy Blue Cap, White Pom Pom" owners="S Darby, Ms B Wenman, R Cayad, Mrs J Whiteley, T Cottam, Ms J Creamer, W Mansbridge, P Papaconstantinou, J Kelly, S Dwain, A Jones, R Panayi, K Karanikolos, A Papaconstantinou, D &amp; K O'Brien Racing (Mgr: D K O'Brien), Two Nations (Mgr: P C Beyerman) &amp; Hopewyn Racing (Mgr: G M Manthey)" dob="2012-11-02T00:00:00" age="4" sex="F" career="12-2-2-1 $289400.00" thistrack="2-0-0-0" thisdistance="2-1-0-0 $163200.00" goodtrack="8-2-1-1 $277975.00" heavytrack="1-0-1-0 $7925.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="3-0-1-0 $7925.00" secondup="2-0-1-0 $16750.00" mindistancewin="0" maxdistancewin="0" finished="8" weightvariation="0" variedweight="58" decimalmargin="6.60" penalty="0" pricestarting="$91.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="12" saddlecloth="12" horse="Mawahibb" id="186932" idnumber="" regnumber="" blinkers="1" trainernumber="77974" trainersurname="Hawkes" trainerfirstname="Michael" trainertrack="Rosehill" rsbtrainername="Michael, Wayne &amp; John Hawkes" jockeynumber="343" jockeysurname="Oliver" jockeyfirstname="Damien" barrier="7" weight="54" rating="70" description="B C 3 Magic Albert x Eye for Fun (Snitzel)" colours="Yellow, Royal Blue Vee And Cap" owners="Sheikh Mohammed Bin Khalifa Al Maktoum" dob="2012-08-31T00:00:00" age="4" sex="C" career="9-1-3-3 $107675.00" thistrack="0-0-0-0" thisdistance="1-0-1-0 $7200.00" goodtrack="5-0-2-2 $40650.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="4-0-1-2 $27125.00" secondup="3-1-1-1 $69600.00" mindistancewin="0" maxdistancewin="0" finished="9" weightvariation="0" variedweight="54" decimalmargin="8.20" penalty="0" pricestarting="$7.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="3" saddlecloth="3" horse="Kimberley Star" id="187337" idnumber="" regnumber="" blinkers="1" trainernumber="77973" trainersurname="Waterhouse" trainerfirstname="Gai" trainertrack="Randwick" rsbtrainername="Gai Waterhouse" jockeynumber="40133" jockeysurname="Shinn" jockeyfirstname="Blake" barrier="6" weight="58" rating="84" description="B F 3 Star Witness x Kiss the Sun (Anabaa(USA))" colours="White, Red Yoke And Hooped Sleeves, Red Cap" owners="S J Murray, Mrs L M Murray, B T Ireland, Mrs P Ireland, S M Gorry &amp; M A Gorry" dob="2012-10-08T00:00:00" age="4" sex="F" career="13-3-3-4 $331985.00" thistrack="8-1-2-2 $194150.00" thisdistance="4-2-0-2 $167500.00" goodtrack="5-0-2-1 $78350.00" heavytrack="3-2-0-1 $105735.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="3-0-0-1 $12500.00" secondup="3-0-2-1 $78960.00" mindistancewin="0" maxdistancewin="0" finished="10" weightvariation="0" variedweight="58" decimalmargin="8.50" penalty="0" pricestarting="$5.50F" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="15" saddlecloth="15" horse="Dixie Chick" id="197658" idnumber="" regnumber="" blinkers="0" trainernumber="58150" trainersurname="Pfieffer" trainerfirstname="David" trainertrack="Warwick Farm" rsbtrainername="David Pfieffer" jockeynumber="51661" jockeysurname="Berry" jockeyfirstname="Tommy" barrier="13" weight="54" rating="67" description="B F 3 Star Witness x Dixie Paradise(USA) (Dixie Union(USA))" colours="White, Royal Blue Halves And Epaulettes, Halved Sleeves, Royal Blue Cap" owners="Mrs V L Waters, G C Waters, M Carr, Mrs B Maxfield, Mrs K Newcombe, G Newcombe, G Rositano, Mrs K Polinelli, C Koskinas, C Iaria, F Iaria, H G Blight &amp; Mrs J A Blight" dob="2012-11-23T00:00:00" age="4" sex="F" career="3-2-0-0 $52160.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="3-2-0-0 $52160.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="1-1-0-0 $17250.00" secondup="1-0-0-0 $1885.00" mindistancewin="0" maxdistancewin="0" finished="11" weightvariation="0" variedweight="54" decimalmargin="9.00" penalty="0" pricestarting="$19.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="14" saddlecloth="14" horse="Sonic Swish" id="195066" idnumber="" regnumber="" blinkers="1" trainernumber="736" trainersurname="Martin" trainerfirstname="Tim" trainertrack="Rosehill" rsbtrainername="Tim Martin" jockeynumber="2687" jockeysurname="Reith" jockeyfirstname="Christian" barrier="8" weight="54" rating="69" description="B G 3 Nicconi x Decibeel(NZ) (Zabeel(NZ))" colours="Lime Green, Silver Hoops, Sleeves And Cap" owners="B K Racing &amp; Breeding Pty Ltd (Mgr: B J Hunt)" dob="2012-10-24T00:00:00" age="4" sex="G" career="5-1-2-1 $53050.00" thistrack="1-0-1-0 $10025.00" thisdistance="1-0-0-0 $4150.00" goodtrack="3-1-1-0 $41425.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="3-0-2-1 $15875.00" secondup="1-1-0-0 $33025.00" mindistancewin="0" maxdistancewin="0" finished="12" weightvariation="0" variedweight="54" decimalmargin="9.30" penalty="0" pricestarting="$13.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="2" saddlecloth="2" horse="Puritan" id="202730" idnumber="" regnumber="" blinkers="0" trainernumber="69250" trainersurname="Warren" trainerfirstname="Jason" trainertrack="Mornington" rsbtrainername="Jason Warren" jockeynumber="48503" jockeysurname="Williams" jockeyfirstname="Craig" barrier="15" weight="58.5" rating="81" description="B G 3 Beneteau x Mina (Dracula)" colours="Yellow And Navy Blue Stripes, Yellow And Navy Blue Stars Cap" owners="M G J Wilson, J D Warren, L B Forkgen, , M H Malherbe, S Crameri, B J Bergin &amp; G Harper, Andwereintoit (Mgr: N Brundell) Aqua Cove Racing (Mgr: M A Hines) &amp; Duc Racing (Mgr: K P J Cremen)" dob="2012-09-07T00:00:00" age="4" sex="G" career="6-2-0-0 $117400.00" thistrack="0-0-0-0" thisdistance="1-0-0-0" goodtrack="5-1-0-0 $104750.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-2-0-0 $103400.00" secondup="2-0-0-0 $12750.00" mindistancewin="0" maxdistancewin="0" finished="13" weightvariation="0" variedweight="58.5" decimalmargin="9.40" penalty="0" pricestarting="$18.00" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
   </race>

So for each race class

<race id="211911"

append it into its sub-elements called nomination as raceid so nomination becomes

<nomination raceid="211911" number="1" saddlecloth="1" horse="Prized Icon" id="198206" idnumber=""
...
<nomination raceid="211911" number="4" saddlecloth="4" horse="Crown Him" id="202278" idnumber=""

Then repeat this for <race id="211912" and the nomination that are its children.

like image 986
sayth Avatar asked Apr 22 '16 11:04

sayth


2 Answers

You could traverse over all the race tags, get the id and add it to the nomination:

d = pq(html)
for race_el in d('race'):
    race = pq(race_el)
    race_id = race.attr('id')

    # get the nominations and add the attribute

    for nom_el in race.items('nomination'):
        pq(nom_el).attr('raceid', race_id)
like image 165
AKS Avatar answered Sep 28 '22 01:09

AKS


I suggest you use Beautiful Soup. It does a great job on tasks like this.

from bs4 import BeautifulSoup
soup = BeautifulSoup(YOUR_HTML_HERE, 'html.parser')
for race in soup.find_all('race'):
    raceid = race['id']
    # recursive=False because you said you only want direct children.
    # omit recursive=False if you want all nomination tags within each race.
    for nomination in race.find_all('nomination', recursive=False):
        nomination['raceid'] = raceid
print(soup.prettify("latin-1"))

And you're done.

like image 31
Joseph Avatar answered Sep 28 '22 03:09

Joseph