使用boto,可以非常容易地解析使用boto.mws.connection和list_orders检索的数据,并隔离特定的数据块,例如订单号:
from boto.mws.connection import MWSConnection
merchantId = 'XXXXXXXXXXX'
marketplaceId = 'XXXXXXXXXXX'
accessKeyId = 'XXXXXXXXXXX'
secretKey = 'XXXXXXXXXXX'
mws = MWSConnection(accessKeyId, secretKey, Merchant=merchantId)
# ListMatchingProducts
a = mws.list_orders(CreatedAfter='2018-05-24T12:00:00Z', MarketplaceId = [marketplaceId])
# retrieve order number within parsed response
a_orderid = a.ListOrdersResult.Orders.Order[0].AmazonOrderId
print(a_orderid)输出amazon订单号:
123-456789-123456相反,如果要使用get_matching_product_for_id操作解析和隔离特定数据,那么就可以为特定的EAN产品ID获取相应的ASIN:
# GetMatchingProductForId (retrieving product info using EAN code)
b = mws.get_matching_product_for_id(MarketplaceId=marketplaceId,IdType="EAN",IdList=["5705260045710"])
# retrieve ASIN for product within result
b_asin = b.GetMatchingProductForIdResult.Products.Product.MarketplaceASIN引发以下错误:
Traceback (most recent call last):
File "C:\Users\alexa\Desktop\API_Amazon_get_matching_product_for_id.py", line 20, in <module>
b_asin = b.GetMatchingProductForIdResult.Products.Product.MarketplaceASIN
AttributeError: 'list' object has no attribute 'Products'有人能找出原因吗?或者有更好的方法来解析boto.mws.connection响应?
发布于 2018-06-02 13:45:21
答案在您的错误消息中。我已经有一段时间没有使用boto了,但是即使不尝试运行您的示例,您也可以看出问题在这里:
b_asin = b.GetMatchingProductForIdResult.Products.Product.MarketplaceASIN错误说:
AttributeError: 'list' object has no attribute 'Products'回头看,我们可以看出python正在尝试访问一个名为Products的属性,但对象是一个列表。
所以b.GetMatchingProductForIdResult是一个列表。试试print吧,看看你得到了什么。迭代它并打印元素或打印第一个元素的dir来查看每个元素的属性。
print(dir(b.GetMatchingProductForIdResult[0]))追忆是你的朋友,学会它,热爱它,生活它。
现在具体到MWS:
Amazon提供了一个描述响应在这里发现的的xsd文件。这应该能告诉你你到底在处理什么。更广泛地说,它描述了元素这里。
发布于 2018-06-02 14:11:25
正如@Verbal_Kint指出的那样,解决方案就在答案中。使用上面的示例,可以通过挖掘树并在需要时将属性作为列表来检索ASIN。我还不太清楚为什么有些属性是列表,而有些则不是,但是在这个阶段,快速的尝试和错误使我能够找到解决方案:
b_asin = b.GetMatchingProductForIdResult[0].Products.Product[0].Identifiers.MarketplaceASIN.ASIN
print(b_asin)https://stackoverflow.com/questions/50657403
复制相似问题