使用ElementTree Python3解析XML

使用ElementTree Python3解析XML,python,xml,Python,Xml,我真的很难解析。我整个星期都在做这件事,但进展甚微。 这里是我正在处理的一个XML示例,它是我从AmazonAPI得到的一个调用。我想收集的是ASIN、ItemCondition、FulfillmentChannel、SellerFeedbackCount、ListingPrice和Shipping。稍后,我想将此信息保存到csv文件中 <?xml version="1.0"?> <GetLowestOfferListingsForASINResponse xmlns="htt

我真的很难解析。我整个星期都在做这件事,但进展甚微。 这里是我正在处理的一个XML示例,它是我从AmazonAPI得到的一个调用。我想收集的是ASIN、ItemCondition、FulfillmentChannel、SellerFeedbackCount、ListingPrice和Shipping。稍后,我想将此信息保存到csv文件中

<?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestOfferListingsForASINResult ASIN="B00CBY103E" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
        <ASIN>**B00CBY103E**</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <LowestOfferListings>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>**New**</ItemCondition>
          <ItemSubcondition>New</ItemSubcondition>
          <FulfillmentChannel>**Merchant**</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>4</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>**163**</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>19.99</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>**19.99**</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>**0.00**</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>True</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>New</ItemCondition>
          <ItemSubcondition>New</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>90-94%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>5486</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>19.99</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>19.99</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>New</ItemCondition>
          <ItemSubcondition>New</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>1188</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>19.99</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>19.99</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>New</ItemCondition>
          <ItemSubcondition>New</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>3-7 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>2240</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>23.97</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>19.99</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>3.98</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>New</ItemCondition>
          <ItemSubcondition>New</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>8-13 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>90-94%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>4377</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>27.99</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>19.99</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>8.00</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
    </LowestOfferListings>
  </Product>
</GetLowestOfferListingsForASINResult>
<GetLowestOfferListingsForASINResult ASIN="B00ERXG4TM" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
        <ASIN>B00ERXG4TM</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <LowestOfferListings>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>New</ItemCondition>
          <ItemSubcondition>New</ItemSubcondition>
          <FulfillmentChannel>Amazon</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>80</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>32.95</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>32.95</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>New</ItemCondition>
          <ItemSubcondition>New</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>80</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>32.95</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>32.95</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
    </LowestOfferListings>
  </Product>
</GetLowestOfferListingsForASINResult>
<ResponseMetadata>
  <RequestId>362ff218-44da-4c1c-97d0-d8cb3ce81bef</RequestId>
</ResponseMetadata>
</GetLowestOfferListingsForASINResponse>
但是,此解析仅提供缺少几行数据的以下内容:

B00CBY103E New Merchant 163
B00ERXG4TM New Amazon 80
从外观上看,我的代码只是从每个产品/ASIN的第一个“LowsTofferList”收集数据,并跳过其余的。我需要做什么来收集“LowstofferList”的完整列表

另一方面,这个XML文档只是整个文档大小的一小部分。elementtree是否会在10倍于此大小的文档中出现任何问题

~~更新~~

我离得更近了。这是我的代码和我得到的输出

Competitors = root.findall('.//{http://mws.amazonservices.com/schema/Products/2011-10-01}LowestOfferListing')
for Competitor in Competitors:
    FeedBack = Competitor.find('{http://mws.amazonservices.com/schema/Products/2011-10-01}SellerFeedbackCount').text
    Condition = Competitor.find('{http://mws.amazonservices.com/schema/Products/2011-10-01}Qualifiers/{http://mws.amazonservices.com/schema/Products/2011-10-01}ItemCondition').text
    Fulfillment = Competitor.find('{http://mws.amazonservices.com/schema/Products/2011-10-01}Qualifiers/{http://mws.amazonservices.com/schema/Products/2011-10-01}FulfillmentChannel').text
    Price = Competitor.find('{http://mws.amazonservices.com/schema/Products/2011-10-01}Price/{http://mws.amazonservices.com/schema/Products/2011-10-01}ListingPrice/{http://mws.amazonservices.com/schema/Products/2011-10-01}Amount').text
    Shipping = Competitor.find('{http://mws.amazonservices.com/schema/Products/2011-10-01}Price/{http://mws.amazonservices.com/schema/Products/2011-10-01}Shipping/{http://mws.amazonservices.com/schema/Products/2011-10-01}Amount').text
    print(FeedBack, Condition, Fulfillment, Price, Shipping)

163 New Merchant 19.99 0.00
5486 New Merchant 19.99 0.00
1188 New Merchant 19.99 0.00
2240 New Merchant 19.99 3.98
4377 New Merchant 19.99 8.00
80 New Amazon 32.95 0.00
80 New Merchant 32.95 0.00
除了数据缺少相关Asin编号这一事实外,输出是正确的。我仍然无法得到Asin值。我假设这是因为我的for循环在位于ASIN下面的lowerstofferlisting上循环。如何获取这些Asin编号?
谢谢你的帮助

当您执行以下操作时:

 LowestOffer.getElementsByTagName("Price")
它将返回一个数组。如果此数组的长度为零,则数据不存在。你可以做:

def get_data(elem, elem_name):
     results = elem.getElementsByTagName(elem_name)
     if results:
           return result[0]
     else:
           return None

 get_data(LowestOffer, "Price")

问题是什么?基本上,我不知道在ClientError树下的节点更改后如何解析数据。告诉我们您正在做什么,一些代码片段会很好地看到,以及解析后您希望接收什么。不要使用minidom,我切换到ElementTree,它可以更容易地处理它-好的,切换到ElementTree,但由于某种原因,我无法迭代日期并获得我想要的。他说的是属性的值,而不是特定的标记名
def get_data(elem, elem_name):
     results = elem.getElementsByTagName(elem_name)
     if results:
           return result[0]
     else:
           return None

 get_data(LowestOffer, "Price")