繁体   English   中英

如何提取Python中elementtree中的多个xml标签?

[英]How to extract mutiple xml tags in elementtree in Python?

我的xml数据如下所示。 我正在尝试为"CUTTER/CANNER""FROZEN CUTTER/CANNER"提取"weighted_avg""change_in_wtd_avg" " 。 为此,我有以下代码,我只能在其中打印来自"CUTTER/CANNER""FROZEN CUTTER/CANNER"的数据。 我不确定如何从同一个 output 中的"CUTTER/CANNER""FROZEN CUTTER/CANNER"获取数据。我是xmlpython ElementTree的新手。

XML 资料

b'<?xml version=\'1.0\' encoding=\'UTF-8\'?><results exportTime="2021-01-03 13:16:25 CDT"><report label="National" slug="LM_XB001"><record report_date="01/24/2009" narrative="null"><report label="Current Cutout Value/Change"><record current_cutout_value="232.67" change_cutout_value="-.03"/></report><report label="Items"><record item_desc="90% lean  " current_price="276.93" current_value="154.64" change_value=".00"/><record item_desc="100% lean inside round  " current_price="563.93" current_value="13.08" change_value=".09"/><record item_desc="100% lean, flats and eyes  " current_price="429.52" current_value="9.96" change_value=".00"/><record item_desc="100% lean, S.P.B.  " current_price="431.54" current_value="21.58" change_value=".00"/><record item_desc="Chuck Tender  " current_price="311.27" current_value="3.11" change_value="-.02"/><record item_desc="Knuckle  4-7 lbs." current_price="323.60" current_value="8.19" change_value=".01"/><record item_desc="Tenderloin  2-3 lbs." current_price="442.55" current_value="2.35" change_value="-.06"/><record item_desc="Tenderloin  3-4 lbs." current_price="663.86" current_value="4.91" change_value=".00"/><record item_desc="Ribeye Roll  4-6 lbs." current_price="405.00" current_value="4.25" change_value=".00"/><record item_desc="Ribeye Roll  6-8 lbs." current_price="392.05" current_value="2.90" change_value="-.06"/><record item_desc="Ribeye Roll  8up lbs." current_price="435.00" current_value="3.18" change_value=".00"/><record item_desc="Flank Steak  " current_price="367.34" current_value="1.54" change_value=".01"/><record item_desc="Kidney, Edible  " current_price="40.00" current_value=".18" change_value=".00"/><record item_desc="Fat, inedible  " current_price="44.72" current_value="1.04" change_value=".00"/><record item_desc="Bone  " current_price="7.32" current_value="1.76" change_value=".00"/></report><report label="Current Volume"><record boner_volume_loads="17.31" cutter_volume_loads="4.11" bbcc_volume_loads="19.47" lean_volume_loads="19.09" frozen_volume_loads="4.61" boner_volume_pounds="692,580" cutter_volume_pounds="164,565" bbcc_volume_pounds="778,700" lean_volume_pounds="763,409" frozen_volume_pounds="184,527"/></report><report label="BONER/BREAKER"><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="11" total_pounds="67,507" weighted_avg="496.91" change_in_wtd_avg="4.35"/><record item_desc="Rib, ribeye roll, 8-10 lbs. (112)" number_trades="32" total_pounds="45,014" weighted_avg="563.10" change_in_wtd_avg="-8.29"/><record item_desc="Rib, ribeye roll, 10-up lbs. (112)" number_trades="52" total_pounds="110,549" weighted_avg="523.10" change_in_wtd_avg="-6.89"/><record item_desc="Rib, ribeye roll-lip-on, 8-dn lbs. (112A)" number_trades="9" total_pounds="14,619" weighted_avg="745.28" change_in_wtd_avg=".72"/><record item_desc="Rib, ribeye roll-lip-on, 8-up lbs. (112A)" number_trades="18" total_pounds="23,518" weighted_avg="590.82" change_in_wtd_avg="-8.21"/><record item_desc="Chuck, boneless 85% ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Chuck, brisket (120)" number_trades="13" total_pounds="21,366" weighted_avg="320.60" change_in_wtd_avg="-.72"/><record item_desc="Round, top inside, 10-dn lbs. (168)" number_trades="13" total_pounds="43,841" weighted_avg="288.11" change_in_wtd_avg="-3.57"/><record item_desc="Round, top inside, 10-up lbs. (168)" number_trades="5" total_pounds="4,188" weighted_avg="378.20" change_in_wtd_avg=".00"/><record item_desc="Round, top inside c-off, 8-10 lbs. (169A)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 10-14 lbs. (169A)" number_trades="14" total_pounds="160,462" weighted_avg="426.16" change_in_wtd_avg=".00"/><record item_desc="Round, top inside c-off, 14-up lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, outside round (171B)" number_trades="6" total_pounds="22,900" weighted_avg="432.18" change_in_wtd_avg=".00"/><record item_desc="Round, eye of round (171C)" number_trades="37" total_pounds="69,061" weighted_avg="418.83" change_in_wtd_avg="1.02"/><record item_desc="Loin, Semi-Bnls Short Loin, 13-dn lbs. ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, Semi-Bnls Short Loin, 13-up lbs. ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 7-9 lbs. (180)" number_trades="5" total_pounds="3,232" weighted_avg="337.92" change_in_wtd_avg=".53"/><record item_desc="Loin, strip, bnls, 9-up lbs. (180)" number_trades="28" total_pounds="36,542" weighted_avg="364.57" change_in_wtd_avg="-8.85"/><record item_desc="Loin, sirloin butt (182)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, top sirloin butt (184)" number_trades="17" total_pounds="29,315" weighted_avg="316.51" change_in_wtd_avg=".57"/><record item_desc="Loin, bottom sirloin butt flap, bnls. (185A)" number_trades="13" total_pounds="36,343" weighted_avg="376.82" change_in_wtd_avg="9.17"/><record item_desc="Loin, butt tender, peeled (191A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="CUTTER/CANNER"><record item_desc="Rib, ribeye roll, 4-6 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="42" total_pounds="164,565" weighted_avg="389.13" change_in_wtd_avg="-11.84"/><record item_desc="Rib, ribeye roll, 8-up lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="100% LEAN"><record item_desc="Inside round - Combo" number_trades="16" total_pounds="101,110" weighted_avg="561.59" change_in_wtd_avg="1.95"/><record item_desc="Inside round - Boxed" number_trades="27" total_pounds="40,350" weighted_avg="569.80" change_in_wtd_avg="8.22"/><record item_desc="Outside round" number_trades="4" total_pounds="33,558" weighted_avg="429.74" change_in_wtd_avg="null"/><record item_desc="Eye of round" number_trades="19" total_pounds="30,408" weighted_avg="435.41" change_in_wtd_avg="-.74"/><record item_desc="Flats and eyes - Combo" number_trades="14" total_pounds="206,885" weighted_avg="428.61" change_in_wtd_avg="-.30"/><record item_desc="Flats and eyes - Boxed" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Striploin" number_trades="81" total_pounds="243,034" weighted_avg="466.56" change_in_wtd_avg="-1.73"/><record item_desc="S.P.B. - Combo" number_trades="12" total_pounds="108,064" weighted_avg="431.54" change_in_wtd_avg="-.06"/><record item_desc="S.P.B. - Boxed" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="B/B and C/C"><record item_desc="Chuck, chuck tender (116B)" number_trades="30" total_pounds="73,521" weighted_avg="311.27" change_in_wtd_avg="-2.08"/><record item_desc="Round, knuckle, peeled (167A)" number_trades="39" total_pounds="199,293" weighted_avg="323.60" change_in_wtd_avg=".28"/><record item_desc="Loin, tenderloin, 2-3 lbs. (190)" number_trades="6" total_pounds="8,983" weighted_avg="447.16" change_in_wtd_avg="-12.52"/><record item_desc="Loin, tenderloin, 3-4 lbs. (190)" number_trades="43" total_pounds="69,979" weighted_avg="663.86" change_in_wtd_avg=".20"/><record item_desc="Loin, tenderloin, 4-5 lbs. (190)" number_trades="169" total_pounds="212,729" weighted_avg="773.68" change_in_wtd_avg="-4.71"/><record item_desc="Loin, tenderloin, 5-up lbs. (190)" number_trades="37" total_pounds="43,056" weighted_avg="617.55" change_in_wtd_avg="13.85"/><record item_desc="Flank, flank steak (193)" number_trades="82" total_pounds="116,533" weighted_avg="390.93" change_in_wtd_avg="1.18"/><record item_desc="Flank, rough ()" number_trades="44" total_pounds="54,606" weighted_avg="322.56" change_in_wtd_avg="-5.70"/></report><report label="FROZEN BONER/BREAKER"><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 8-10 lbs. (112)" number_trades="6" total_pounds="16,005" weighted_avg="473.41" change_in_wtd_avg=".00"/><record item_desc="Rib, ribeye roll, 10-up lbs. (112)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll-lip-on, 8-dn lbs. (112A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll-lip-on, 8-up lbs. (112A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 8-10 lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 10-14 lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 14-up lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 7-9 lbs. (180)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 9-up lbs. (180)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="FROZEN CUTTER/CANNER"><record item_desc="Rib, ribeye roll, 4-6 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="5" total_pounds="85,800" weighted_avg="397.65" change_in_wtd_avg="-2.26"/><record item_desc="Rib, ribeye roll, 8-up lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="FROZEN B/B and C/C"><record item_desc="Loin, tenderloin, 2-3 lbs. (190)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 3-4 lbs. (190)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 4-5 lbs. (190)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 5-up lbs. (190)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Flank, flank steak (193)" number_trades="10" total_pounds="77,922" weighted_avg="332.06" change_in_wtd_avg="-.32"/></report></record></report></results>'

代码:

import xml.etree.ElementTree as ET

r1 = requests.get(xml link)
root1 = ET.fromstring(r1.content)

    for child in root1.find(".//report[@label='CUTTER/CANNER']"):
        print(child.attrib.get('weighted_avg'),child.attrib.get('change_in_wtd_avg'))

预计 Output:

null     null
399.75   .65
457.78    .00
.00      null
413.49   3.73
.00      null

我认为你应该运行for -loop 两次

for child in root1.find(".//report[@label='CUTTER/CANNER']"):
    print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))    

for child in root1.find(".//report[@label='FROZEN CUTTER/CANNER']"):
    print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))    

它不应该重复值。

但是如果有更多'CUTTER/CANNER'和更多'FROZEN CUTTER/CANNER' ,我会使用findall()而不是find()来获取所有元素

for item in root1.findall(".//report[@label='CUTTER/CANNER']"):
    for child in item:
        print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))

for item in root1.findall(".//report[@label='FROZEN CUTTER/CANNER']"):
    for child in item:
        print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))

使用模块lxml您可以使用 function xpath()而不是find()并且它可以使用orcontains()

xml的文档显示XPath 支持,它不支持所有 XPath 功能)

for item in root1.xpath(".//report[@label='CUTTER/CANNER' or @label='FROZEN CUTTER/CANNER']"):
    for child in item:
        print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))
for item in root1.xpath(".//report[contains(@label, 'CUTTER/CANNER')]"):
    for child in item:
        print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))

最小的工作示例

content = b'<?xml version=\'1.0\' encoding=\'UTF-8\'?><results exportTime="2021-01-03 13:16:25 CDT"><report label="National" slug="LM_XB001"><record report_date="01/24/2009" narrative="null"><report label="Current Cutout Value/Change"><record current_cutout_value="232.67" change_cutout_value="-.03"/></report><report label="Items"><record item_desc="90% lean  " current_price="276.93" current_value="154.64" change_value=".00"/><record item_desc="100% lean inside round  " current_price="563.93" current_value="13.08" change_value=".09"/><record item_desc="100% lean, flats and eyes  " current_price="429.52" current_value="9.96" change_value=".00"/><record item_desc="100% lean, S.P.B.  " current_price="431.54" current_value="21.58" change_value=".00"/><record item_desc="Chuck Tender  " current_price="311.27" current_value="3.11" change_value="-.02"/><record item_desc="Knuckle  4-7 lbs." current_price="323.60" current_value="8.19" change_value=".01"/><record item_desc="Tenderloin  2-3 lbs." current_price="442.55" current_value="2.35" change_value="-.06"/><record item_desc="Tenderloin  3-4 lbs." current_price="663.86" current_value="4.91" change_value=".00"/><record item_desc="Ribeye Roll  4-6 lbs." current_price="405.00" current_value="4.25" change_value=".00"/><record item_desc="Ribeye Roll  6-8 lbs." current_price="392.05" current_value="2.90" change_value="-.06"/><record item_desc="Ribeye Roll  8up lbs." current_price="435.00" current_value="3.18" change_value=".00"/><record item_desc="Flank Steak  " current_price="367.34" current_value="1.54" change_value=".01"/><record item_desc="Kidney, Edible  " current_price="40.00" current_value=".18" change_value=".00"/><record item_desc="Fat, inedible  " current_price="44.72" current_value="1.04" change_value=".00"/><record item_desc="Bone  " current_price="7.32" current_value="1.76" change_value=".00"/></report><report label="Current Volume"><record boner_volume_loads="17.31" cutter_volume_loads="4.11" bbcc_volume_loads="19.47" lean_volume_loads="19.09" frozen_volume_loads="4.61" boner_volume_pounds="692,580" cutter_volume_pounds="164,565" bbcc_volume_pounds="778,700" lean_volume_pounds="763,409" frozen_volume_pounds="184,527"/></report><report label="BONER/BREAKER"><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="11" total_pounds="67,507" weighted_avg="496.91" change_in_wtd_avg="4.35"/><record item_desc="Rib, ribeye roll, 8-10 lbs. (112)" number_trades="32" total_pounds="45,014" weighted_avg="563.10" change_in_wtd_avg="-8.29"/><record item_desc="Rib, ribeye roll, 10-up lbs. (112)" number_trades="52" total_pounds="110,549" weighted_avg="523.10" change_in_wtd_avg="-6.89"/><record item_desc="Rib, ribeye roll-lip-on, 8-dn lbs. (112A)" number_trades="9" total_pounds="14,619" weighted_avg="745.28" change_in_wtd_avg=".72"/><record item_desc="Rib, ribeye roll-lip-on, 8-up lbs. (112A)" number_trades="18" total_pounds="23,518" weighted_avg="590.82" change_in_wtd_avg="-8.21"/><record item_desc="Chuck, boneless 85% ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Chuck, brisket (120)" number_trades="13" total_pounds="21,366" weighted_avg="320.60" change_in_wtd_avg="-.72"/><record item_desc="Round, top inside, 10-dn lbs. (168)" number_trades="13" total_pounds="43,841" weighted_avg="288.11" change_in_wtd_avg="-3.57"/><record item_desc="Round, top inside, 10-up lbs. (168)" number_trades="5" total_pounds="4,188" weighted_avg="378.20" change_in_wtd_avg=".00"/><record item_desc="Round, top inside c-off, 8-10 lbs. (169A)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 10-14 lbs. (169A)" number_trades="14" total_pounds="160,462" weighted_avg="426.16" change_in_wtd_avg=".00"/><record item_desc="Round, top inside c-off, 14-up lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, outside round (171B)" number_trades="6" total_pounds="22,900" weighted_avg="432.18" change_in_wtd_avg=".00"/><record item_desc="Round, eye of round (171C)" number_trades="37" total_pounds="69,061" weighted_avg="418.83" change_in_wtd_avg="1.02"/><record item_desc="Loin, Semi-Bnls Short Loin, 13-dn lbs. ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, Semi-Bnls Short Loin, 13-up lbs. ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 7-9 lbs. (180)" number_trades="5" total_pounds="3,232" weighted_avg="337.92" change_in_wtd_avg=".53"/><record item_desc="Loin, strip, bnls, 9-up lbs. (180)" number_trades="28" total_pounds="36,542" weighted_avg="364.57" change_in_wtd_avg="-8.85"/><record item_desc="Loin, sirloin butt (182)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, top sirloin butt (184)" number_trades="17" total_pounds="29,315" weighted_avg="316.51" change_in_wtd_avg=".57"/><record item_desc="Loin, bottom sirloin butt flap, bnls. (185A)" number_trades="13" total_pounds="36,343" weighted_avg="376.82" change_in_wtd_avg="9.17"/><record item_desc="Loin, butt tender, peeled (191A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="CUTTER/CANNER"><record item_desc="Rib, ribeye roll, 4-6 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="42" total_pounds="164,565" weighted_avg="389.13" change_in_wtd_avg="-11.84"/><record item_desc="Rib, ribeye roll, 8-up lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="100% LEAN"><record item_desc="Inside round - Combo" number_trades="16" total_pounds="101,110" weighted_avg="561.59" change_in_wtd_avg="1.95"/><record item_desc="Inside round - Boxed" number_trades="27" total_pounds="40,350" weighted_avg="569.80" change_in_wtd_avg="8.22"/><record item_desc="Outside round" number_trades="4" total_pounds="33,558" weighted_avg="429.74" change_in_wtd_avg="null"/><record item_desc="Eye of round" number_trades="19" total_pounds="30,408" weighted_avg="435.41" change_in_wtd_avg="-.74"/><record item_desc="Flats and eyes - Combo" number_trades="14" total_pounds="206,885" weighted_avg="428.61" change_in_wtd_avg="-.30"/><record item_desc="Flats and eyes - Boxed" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Striploin" number_trades="81" total_pounds="243,034" weighted_avg="466.56" change_in_wtd_avg="-1.73"/><record item_desc="S.P.B. - Combo" number_trades="12" total_pounds="108,064" weighted_avg="431.54" change_in_wtd_avg="-.06"/><record item_desc="S.P.B. - Boxed" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="B/B and C/C"><record item_desc="Chuck, chuck tender (116B)" number_trades="30" total_pounds="73,521" weighted_avg="311.27" change_in_wtd_avg="-2.08"/><record item_desc="Round, knuckle, peeled (167A)" number_trades="39" total_pounds="199,293" weighted_avg="323.60" change_in_wtd_avg=".28"/><record item_desc="Loin, tenderloin, 2-3 lbs. (190)" number_trades="6" total_pounds="8,983" weighted_avg="447.16" change_in_wtd_avg="-12.52"/><record item_desc="Loin, tenderloin, 3-4 lbs. (190)" number_trades="43" total_pounds="69,979" weighted_avg="663.86" change_in_wtd_avg=".20"/><record item_desc="Loin, tenderloin, 4-5 lbs. (190)" number_trades="169" total_pounds="212,729" weighted_avg="773.68" change_in_wtd_avg="-4.71"/><record item_desc="Loin, tenderloin, 5-up lbs. (190)" number_trades="37" total_pounds="43,056" weighted_avg="617.55" change_in_wtd_avg="13.85"/><record item_desc="Flank, flank steak (193)" number_trades="82" total_pounds="116,533" weighted_avg="390.93" change_in_wtd_avg="1.18"/><record item_desc="Flank, rough ()" number_trades="44" total_pounds="54,606" weighted_avg="322.56" change_in_wtd_avg="-5.70"/></report><report label="FROZEN BONER/BREAKER"><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 8-10 lbs. (112)" number_trades="6" total_pounds="16,005" weighted_avg="473.41" change_in_wtd_avg=".00"/><record item_desc="Rib, ribeye roll, 10-up lbs. (112)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll-lip-on, 8-dn lbs. (112A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll-lip-on, 8-up lbs. (112A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 8-10 lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 10-14 lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 14-up lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 7-9 lbs. (180)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 9-up lbs. (180)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="FROZEN CUTTER/CANNER"><record item_desc="Rib, ribeye roll, 4-6 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="5" total_pounds="85,800" weighted_avg="397.65" change_in_wtd_avg="-2.26"/><record item_desc="Rib, ribeye roll, 8-up lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="FROZEN B/B and C/C"><record item_desc="Loin, tenderloin, 2-3 lbs. (190)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 3-4 lbs. (190)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 4-5 lbs. (190)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 5-up lbs. (190)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Flank, flank steak (193)" number_trades="10" total_pounds="77,922" weighted_avg="332.06" change_in_wtd_avg="-.32"/></report></record></report></results>'

def version1(content):
    print('--- version1 ---')
    
    import xml.etree.ElementTree as ET

    #r1 = requests.get(xml link)
    #root1 = ET.fromstring(r1.content)

    root1 = ET.fromstring(content)

    for item in root1.findall(".//report[@label='CUTTER/CANNER']"):
        for child in item:
            print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))

    for item in root1.findall(".//report[@label='FROZEN CUTTER/CANNER']"):
        for child in item:
            print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))


def version2(content):
    print('--- version2 ---')

    import lxml.etree as ET

    #r1 = requests.get(xml link)
    #root1 = ET.fromstring(r1.content)

    root1 = ET.fromstring(content)

    for item in root1.xpath(".//report[contains(@label, 'CUTTER/CANNER')]"):
        for child in item:
            print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))


def version3(content):
    print('--- version3 ---')

    import lxml.etree as ET

    #r1 = requests.get(xml link)
    #root1 = ET.fromstring(r1.content)

    root1 = ET.fromstring(content)

    for item in root1.xpath(".//report[@label='CUTTER/CANNER' or @label='FROZEN CUTTER/CANNER']"):
        for child in item:
            print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))

# --- main ---

version1(content)
version2(content)
version3(content)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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