-1

Im having a really hard time with parsing. Ive been working on it all week and have made very little head way. Here is an example of the XML im working on, its a call that I've gotten from an amazon api. What Im trying to gather is the ASIN, ItemCondition, FulfillmentChannel, SellerFeedbackCount, ListingPrice, and Shipping. Later I would like to have this info saved to a csv file.

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

Here is my current code:

import csv
import xml.etree.ElementTree as ET

tree = ET.parse('outputmulti.xml')
root = tree.getroot()
for sku in root.findall('{http://mws.amazonservices.com/schema/Products/2011-10-01}GetLowestOfferListingsForASINResult/{http://mws.amazonservices.com/schema/Products/2011-10-01}Product'):
    asin = sku.find('.//{http://mws.amazonservices.com/schema/Products/2011-10-01}ASIN').text
    Condition = sku.find('.//{http://mws.amazonservices.com/schema/Products/2011-10-01}ItemCondition').text
    Fulfillment = sku.find('.//{http://mws.amazonservices.com/schema/Products/2011-10-01}FulfillmentChannel').text
    FeedBack = sku.find('.//{http://mws.amazonservices.com/schema/Products/2011-10-01}SellerFeedbackCount').text
    print(asin, Condition, Fulfillment, FeedBack)

However this parse only gives the follow which is missing several rows of data:

B00CBY103E New Merchant 163
B00ERXG4TM New Amazon 80

By the looks of it my code is only gathering the data from the first "LowestOfferListing" for each Product/ASIN and skipping the remaining. What do I need to do to gather the a complete list of "LowestOfferListing"?

On a side note this XML document is just a fraction of the size of the full document. Is elementtree going to have any issue going through a document that is 10 times this size.

~~UPDATE~~

Ive gotten much closer. Heres my code and the output I get.

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

The output is correct other then the fact that the data is missing its correlating Asin number. I still cant get the Asin Values. Im assuming that this is because my for loop is looping over the LowestOfferListing which is located below the ASIN. How do I get these Asin numbers? Thanks for the help!!

5
  • 2
    and what's the question? Commented Jan 12, 2015 at 23:59
  • Basically I dont know how to parse the data once the nodes change under the ClientError tree. Commented Jan 13, 2015 at 0:38
  • show us what you are doing, some code snippet would be nice to see and what you want to receive after parsing. Commented Jan 13, 2015 at 0:40
  • don't use minidom, i switched to ElementTree, it can handle it easier - stackoverflow.com/questions/27810825/… Commented Jan 13, 2015 at 23:04
  • okay switched over to ElementTree but for some reason I cant to iterate over the date and get what Im after. Commented Jan 21, 2015 at 20:13

1 Answer 1

0

When you do something like:

 LowestOffer.getElementsByTagName("Price")

It will return an array. If the length of this array is zero then the data does not exist. You can do:

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

 get_data(LowestOffer, "Price")
Sign up to request clarification or add additional context in comments.

1 Comment

he's talking about attribute's value, not about specific tag name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.