2015-07-06 37 views
1

我在Python中使用Boto连接到亚马逊MWS。我已经成功地使用他们的脚本进行连接,但由于我没有完全理解文档,因此在解析响应时遇到了问题,并且在互联网上几乎没有任何示例。我是Python的新手。麻烦分析MWS Boto响应

这是我如何得到MWS我的回应:

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

的第一款产品给出了这样的回应:

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')])) 

我的问题是试图从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') 

在这一点上,我相信它是一个字典对象。

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

由于我是新来这个语言,我无法弄清楚如何获取位于(),如品牌,工作室等

博托有内建的所有信息功能,如响应和ResponseFactory,但当我继续打墙尝试获取品牌等信息时,我再次丢失。

再次感谢您给予的任何帮助。

+0

你有什么版本的Python?你可以使用'type()'或'isinstance()'来查看变量的类型。 –

+0

@ sancho.s - 谢谢 - 使用Python 3.4。当我编码:打印(类型(属性[0]))它给了我一个的响应。博托的文档位于这里,[链接](http://boto.readthedocs.org/en/latest/ref/mws.html),但我无法弄清楚文档是如何引用我来提取细节我需要。 – Michael

+0

我会建议几件事:1)尝试不索引与[0],2)检查可用的方法http://stackoverflow.com/questions/1911281/how-do-i-get-list-of-methods- in-a-python-class,或者http://stackoverflow.com/questions/34439/finding-what-methods-an-object-has,3)用http://stackoverflow.com/questions/152580/检查类型什么是规范的方式来检查py-type-in​​-type, –

回答

0

如果你想“了解详细内容”为输出,你可以尝试Python's Data pretty printer

pprint模块提供了能力“漂亮打印”任意 Python数据结构.. 。

它的输出是非常多的可配置的,你可以去结构的任意深度,与depth=...

如果你想使用其他地方, 我会建议一些事情“细节获得”:

  1. 尽量不要与[0]索引,你可能会失去存储在别处的信息在Attributes
  2. How do I get list of methods in a Python class?Finding what methods an object has检查可用的方法。
  3. 检查类型与What's the canonical way to check for type in python?

通过Attributes获得的信息,您应该能够提取存储在其中的任何信息。

+0

感谢您的回复。但我需要访问数据,而不仅仅是查看数据,将其打印在报告中,并且必须为数百个项目执行此操作。我尝试了你的回应,并且仍然有同样的问题。 pp = pprint.PrettyPrinter(indent = 5) pp.pprint(Attributes)只给我打印出“[{'xml:lang':'en-US'}]” - 仍然无法深入并得到访问上面指定的所有细节。 – Michael

+0

@Michael - 尝试使用'depth = ...'。这至少会告诉你,如果你可以访问数据,即使不检索使用。 –