2015-07-06 36 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 = ...'。這至少會告訴你,如果你可以訪問數據,即使不檢索使用。 –