1

I am using Boto in Python to connect to Amazon MWS. I have successfully connected using their scripts but am having trouble parsing the response as I don't fully understand the documentation, and there are little to no examples on the internet. I am new to Python.

Here is how I get my response from MWS:

mws = MWSConnection(accessKeyId,secretKey,Merchant=merchantId)
response = mws.list_matching_products(MarketplaceId=marketplaceId,Query="Beanie Babies")

The first product gives a response of this:

products = response.ListMatchingProductsResult.Products.Product
print(products[0])
>>>Product{}(Identifiers: ^Identifiers^{}(MarketplaceASIN: ^MarketplaceASIN^{}(MarketplaceId: 'ATVPDKIKX0DER', ASIN: 'B000JK67MQ'), SKUIdentifier: None), Offers: None, CompetitivePricing: [], AttributeSets: ^AttributeSets^{}(ItemAttributes: [ItemAttributes{'xml:lang': 'en-US'}(Brand: 'Beanie Babies', Studio: 'Beanie Babies - Teddy Bears', ItemDimensions: 4.00inchesx10.00inchesx6.00inchesx0.31pounds, Languages: None, Binding: 'Toy', Genre: 'cute pets', Color: 'purple', MaterialType: [], Feature: ['Ty Beanie Baby', 'Princess Bear', 'Purple with purple bow with white flower', 'Does have the tag'], ManufacturerMaximumAge: 36{'Units': 'months'}, OperatingSystem: [], Artist: [], Director: [], ProductTypeName: 'TOYS_AND_GAMES', Creator: [], Edition: '1997', Model: '4300', SmallImage: Image{}(Height: 75{'Units': 'pixels'}, Width: 75{'Units': 'pixels'}, URL: 'http://ecx.images-amazon.com/images/I/4193jH4e35L._SL75_.jpg'), GemType: [], PackageDimensions: 0.90inchesx7.40inchesx4.50inchesx0.40pounds, PackageQuantity: '1', ListPrice: None, Actor: [], Platform: [], Manufacturer: 'Beanie Babies - Teddy Bears', PartNumber: '4300', ProductGroup: 'Toy', MediaType: [], IsMemorabilia: 'false', Label: 'Beanie Babies - Teddy Bears', ManufacturerMinimumAge: 36{'Units': 'months'}, IsAutographed: 'false', IsAdultProduct: 'false', Author: [], Format: [], Title: 'Ty Beanie Babies - Princess Bear', Publisher: 'Beanie Babies - Teddy Bears')]), LowestOfferListings: None, Relationships: ^Relationships^{}(VariationParent: []), SalesRankings: ^SalesRankings^{}(SalesRank: [SalesRank{}(Rank: '60161', ProductCategoryId: 'toy_display_on_website'), SalesRank{}(Rank: '1197', ProductCategoryId: '251943011'), SalesRank{}(Rank: '1609', ProductCategoryId: '11350120011')]))

My Issue is trying to get the details from the ItemAttributes:

Attributes = products[0].AttributeSets.ItemAttributes
print(Attributes[0])
>>>ItemAttributes{'xml:lang': 'en-US'}(Brand: 'Beanie Babies', Studio: 'Beanie Babies - Teddy Bears', ItemDimensions: 4.00inchesx10.00inchesx6.00inchesx0.31pounds, Languages: None, Binding: 'Toy', Genre: 'cute pets', Color: 'purple', MaterialType: [], Feature: ['Ty Beanie Baby', 'Princess Bear', 'Purple with purple bow with white flower', 'Does have the tag'], ManufacturerMaximumAge: 36{'Units': 'months'}, OperatingSystem: [], Artist: [], Director: [], ProductTypeName: 'TOYS_AND_GAMES', Creator: [], Edition: '1997', Model: '4300', SmallImage: Image{}(Height: 75{'Units': 'pixels'}, Width: 75{'Units': 'pixels'}, URL: 'http://ecx.images-amazon.com/images/I/4193jH4e35L._SL75_.jpg'), GemType: [], PackageDimensions: 0.90inchesx7.40inchesx4.50inchesx0.40pounds, PackageQuantity: '1', ListPrice: None, Actor: [], Platform: [], Manufacturer: 'Beanie Babies - Teddy Bears', PartNumber: '4300', ProductGroup: 'Toy', MediaType: [], IsMemorabilia: 'false', Label: 'Beanie Babies - Teddy Bears', ManufacturerMinimumAge: 36{'Units': 'months'}, IsAutographed: 'false', IsAdultProduct: 'false', Author: [], Format: [], Title: 'Ty Beanie Babies - Princess Bear', Publisher: 'Beanie Babies - Teddy Bears')

At this point I believe it is a dictionary object.

print(Attributes[0].values()) 
>>>dict_values(['en-US'])

As I am new to this language I can't figure out how to get all the information that is located in the () such as Brand, Studio, etc.

Boto has some built in functions such as Response and ResponseFactory, but I am lost again as I keep hitting a wall trying to get info such as Brand, etc..

Thank you again for any help you can give in this.

PaulG
  • 13,871
  • 9
  • 56
  • 78
Michael
  • 93
  • 2
  • 10
  • What version of Python do you have? You can use `type()` or `isinstance()` to see the type of a variable. – sancho.s ReinstateMonicaCellio Jul 06 '15 at 11:56
  • @sancho.s - Thanks - Using Python 3.4. When I code : print(type(Attributes[0])) it gives me a response of . The documentation for Boto is located here, [link](http://boto.readthedocs.org/en/latest/ref/mws.html), but I can't figure out how the documentation is referring me to pull the details that I need. – Michael Jul 06 '15 at 12:10
  • I would suggest a few things: 1) try not indexing with [0], 2) Check available methods with http://stackoverflow.com/questions/1911281/how-do-i-get-list-of-methods-in-a-python-class, or http://stackoverflow.com/questions/34439/finding-what-methods-an-object-has, 3) Check type with http://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python, – sancho.s ReinstateMonicaCellio Jul 06 '15 at 12:30
  • @sancho.s. Thanks this really helped. I was able to use the dir(Attributes[0]), that printed out all the attributes (Brand, Author, etc.) and then was able to use hasattr(Attributes[0], "Author") / getattr(Attributes[0], "Author") to get the information I needed. Thank you so much. You also mentioned that I shouldn't be referencing the list item by Attributes[0]. In my script I run a check to see if it exists before calling it, as it would be the only instance to be found. How would you go about finding this? – Michael Jul 06 '15 at 12:49
  • It is ok to index, if you want the contents of Attributes[0]. But you may be losing information stored somewhere else in Attributes... I only meant to warn you about this. I wrote the comment as an answer, as it may help others as well. Please give feedback on the answer itself. – sancho.s ReinstateMonicaCellio Jul 10 '15 at 19:59

1 Answers1

0

If you want "to get the details" for output, you can try Python's Data pretty printer:

The pprint module provides a capability to “pretty-print” arbitrary Python data structures...

Its output is very much configurable, and you can go down the structure to an arbitrary depth, with depth=....

If you want "to get the details" for using it elsewhere, I would suggest a few things:

  1. Try not indexing with [0], you may be losing information stored somewhere else in Attributes.
  2. Check available methods with How do I get list of methods in a Python class?, or Finding what methods an object has.
  3. Check type with What's the canonical way to check for type in python?.

With the information obtained on Attributes you should be able to extract any info stored in it.

Community
  • 1
  • 1
  • 1
    Thanks for the reply. But I need to access the data, not just view it, to print it out on a report and have to do this for hundreds of items. I tried your response and am still having the same issue. pp = pprint.PrettyPrinter(indent=5) pp.pprint(Attributes) only gives me a print out of "[ { 'xml:lang': 'en-US'}]" - still can't get deeper and get access to all the details as specified above. – Michael Jul 06 '15 at 11:24
  • @Michael - Try using `depth=...`. This will at least show you if you can access data, even if not retrieving it for use. – sancho.s ReinstateMonicaCellio Jul 06 '15 at 12:33