在Python中遍历XML

时间:2013-01-14 17:11:36

标签: python xml

我有一些非常“深入”的XML(参见例如底部)并且正在努力使用minidom或elementtree来获取我需要的值 - 任何想法获取粗体值的最简单方法(VALUE_TO_GET )是否会有不同数量的LowestOfferListing标签?我基本上需要为每个LandedPrice获取Amount / SKUIdentifier的第一个实例,并且无法找到这样做的简单(ish)方式!

<?xml version="1.0" ?>
<GetLowestOfferListingsForSKUResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
  <GetLowestOfferListingsForSKUResult SellerSKU="X" 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>X</MarketplaceId>
          <ASIN>X</ASIN>
        </MarketplaceASIN>
        <SKUIdentifier>
          <MarketplaceId>X</MarketplaceId>
          <SellerId>X</SellerId>
          <SellerSKU>**VALUE_TO_GET**</SellerSKU>
        </SKUIdentifier>
      </Identifiers>
      <LowestOfferListings>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>New</ItemCondition>
            <ItemSubcondition>New</ItemSubcondition>
            <FulfillmentChannel>Amazon</FulfillmentChannel>
            <ShipsDomestically>True</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>3</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>**VALUE_TO_GET**</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>Merchant</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>X</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>X</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>X</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>X</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
      </LowestOfferListings>
    </Product>
  </GetLowestOfferListingsForSKUResult>
  <GetLowestOfferListingsForSKUResult SellerSKU="X" status="X">
    <AllOfferListingsConsidered>X</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>X</MarketplaceId>
          <ASIN>X</ASIN>
        </MarketplaceASIN>
        <SKUIdentifier>
          <MarketplaceId>X</MarketplaceId>
          <SellerId>X</SellerId>
          <SellerSKU>**VALUE_TO_GET**</SellerSKU>
        </SKUIdentifier>
      </Identifiers>
      <LowestOfferListings>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>**VALUE_TO_GET**</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>8</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>Merchant</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>4</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
        <LowestOfferListing>
          <Qualifiers>
            <ItemCondition>X</ItemCondition>
            <ItemSubcondition>X</ItemSubcondition>
            <FulfillmentChannel>X</FulfillmentChannel>
            <ShipsDomestically>X</ShipsDomestically>
            <ShippingTime>
              <Max>X</Max>
            </ShippingTime>
            <SellerPositiveFeedbackRating>X</SellerPositiveFeedbackRating>
          </Qualifiers>
          <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
          <SellerFeedbackCount>X</SellerFeedbackCount>
          <Price>
            <LandedPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </LandedPrice>
            <ListingPrice>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </ListingPrice>
            <Shipping>
              <CurrencyCode>X</CurrencyCode>
              <Amount>X</Amount>
            </Shipping>
          </Price>
          <MultipleOffersAtLowestPrice>X</MultipleOffersAtLowestPrice>
        </LowestOfferListing>
      </LowestOfferListings>
    </Product>
  </GetLowestOfferListingsForSKUResult>
  <ResponseMetadata>
    <RequestId>X</RequestId>
  </ResponseMetadata>
</GetLowestOfferListingsForSKUResponse>

1 个答案:

答案 0 :(得分:1)

首先,你的xml似乎没有效果,因为它给出了elemntree的错误,所以我尝试使用lxml.html,这更强大,xpath仍然存在同样的。

import lxml.html as lh
doc=lh.fromstring(xml)
doc.xpath('//sellersku/text()|//product/descendant::amount[1]/text()')

输出:

['**VALUE_TO_GET**',
 '**VALUE_TO_GET**',
 '**VALUE_TO_GET**',
 '**VALUE_TO_GET**']