用ElementTree Python3解析XML

2024-05-19 10:23:20 发布

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

我有一个非常困难的时间与解析。我花了整整一个星期的时间在这方面做了一点努力,但没有取得什么进展。 这是一个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>

以下是我当前的代码:

^{pr2}$

但是,此分析仅给出缺少几行数据的以下内容:

^{3}$

从外观上看,我的代码只是从每个产品/ASIN的第一个“lowstofferlist”中收集数据,而跳过其余的。我需要做些什么来收集一份完整的“Lowstofferlisting”列表?在

另一方面,这个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下面的lowstofferlist上循环。如何得到这些Asin号码? 谢谢你的帮助!!在


Tags: comhttpamazonnewschemaserviceamountprice

热门问题