我在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,但當我繼續打牆嘗試獲取品牌等信息時,我再次丟失。
再次感謝您給予的任何幫助。
你有什麼版本的Python?你可以使用'type()'或'isinstance()'來查看變量的類型。 –
@ sancho.s - 謝謝 - 使用Python 3.4。當我編碼:打印(類型(屬性[0]))它給了我一個的響應。博託的文檔位於這裏,[鏈接](http://boto.readthedocs.org/en/latest/ref/mws.html),但我無法弄清楚文檔是如何引用我來提取細節我需要。 –
Michael
我會建議幾件事: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, –