2013-01-14 108 views
-1

我有一些非常'深'的XML(例如見底部),我正努力使用minidom或者elementtree來獲得我需要的值 - 任何想法到達這個最簡單的方法粗體數值(VALUE_TO_GET)是否會有不同數量的LowestOfferListing標籤?我基本上需要爲每個SKUIdentifier獲得LandedPrice/Amount的第一個實例,並且找不到這樣做的簡單(ish)方式!在Python中遍歷XML

<?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

首先,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**'] 
+1

是公形成的XML。在編輯之前它只是格式不正確。 – mzjn

+0

@root感謝您的回答 - 以前從未使用過lxml,但似乎可能是前進之路。但是,您給出的示例僅爲我返回前兩個** VALUE_TO_GET **。我得到(我認爲)我們正在隔離'// sellersku/text()',並將管道結果找到'// product/descendant'和'amount [1]/text()' - 是否存在一種遞歸的方式來實現這一點,並找到這個的所有實例(因爲實際列表將包括成千上萬的產品)? 謝謝, Matt –

+0

@root對不起,但我不明白你的意見。你是說在xml中有兩個這樣的值?或者表達式'doc.xpath('// sellersku/text()| // product/descendant :: amount [1]/text()')'包含'// sellersku/text()'和' // product'? 這種方法是做我想做的,只不過它只是採取sellersku的第一個實例,並在xml中找到它的數量,而我希望它找到每個sellersku的所有第一個子實例。 我想用它來將這些信息位以sellersku作爲關鍵字和金額作爲值拉入字典。 –