top of page

用 Python × SP-API 检索商品信息 | Amazon Selling Partner API

  • 2天前
  • 讀畢需時 3 分鐘

Selling Partner API(SP-API)是面向Amazon卖家合作伙伴的自动化功能的新一代API套件,是Amazon Marketplace Web Service(MWS)API的演进版本。MWS已提供对Amazon重要功能的程序化访问超过十年。SP-API公开后,将可在目前支持MWS的所有Amazon商店的生产环境中使用。

目前使用MWS的开发者,需要在2024年4月1日之前迁移到SP-API。(截至2023年1月23日的信息)

Selling Partner API for Catalog Items

Catalog-Items的API可以获取目录内商品信息的详情。

环境准备

要在Python中使用SP-API,需要安装python-amazon-sp-api库。我在AWS Lambda中执行Python,因此为了把库安装到Lambda的目标项目中,加上了「-t .」选项

pip install python-amazon-sp-api -t .

执行SP-API需要凭据。把凭据统一存放在credentials中会比较方便。各个值的获取方法将在另一篇文章中介绍。

credentials : {
    "refresh_token": 刷新令牌,
    "lwa_app_id": 客户端ID,
    "lwa_client_secret": 客户端密钥,
    "aws_access_key": AWS访问密钥,
    "aws_secret_key": AWS私有密钥
}

执行API

CatalogItems API提供以下操作。

searchCatalogItems:检索并返回Amazon目录商品与相关信息的列表。

getCatalogItems:获取Amazon目录内商品的详情。

searchCatalogItems

from sp_api.api import CatalogItems
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
	#创建用于使用CatalogItems API的对象
	obj = CatalogItems(marketplace=Marketplaces.JP,
	                   credentials=event['credentials'])
	#设置检索所需的值并获取商品信息
	result = obj.search_catalog_items(keywords='test',
                                    pageSize=1,
                                    brandNames=None,
                                    includedData='images,summaries',
                                    classificationIds='')
	return result.payload

参数说明

keywords:检索关键词

pageSize:每页返回的条数

brandNames:按品牌名筛选(不指定则为None)

includedData:要包含的数据,如images或summaries

classificationIds:按分类ID筛选

执行结果(示例)

{
	"numberOfResults": 682495,
	"refinements": {
		"brands": [
			{
				"numberOfResults": 31634,
				"brandName": "DFV mobile"
			},
			{
				"numberOfResults": 11337,
				"brandName": "HM&CL"
			},
			{
				"numberOfResults": 10035,
				"brandName": "Yise Knight"
			},
			{
				"numberOfResults": 9922,
				"brandName": "House Of Candles"
			},
			{
				"numberOfResults": 7716,
				"brandName": "Independently Published"
			},
			{
				"numberOfResults": 7040,
				"brandName": "QIAOMEL"
			},
			{
				"numberOfResults": 5546,
				"brandName": "Routledge"
			},
			{
				"numberOfResults": 5153,
				"brandName": "XXXXXbrand"
			},
			{
				"numberOfResults": 5070,
				"brandName": "Jpp"
			},
			{
				"numberOfResults": 4890,
				"brandName": "Createspace Independent Publishing Platform"
			}
		],
		"classifications": [
			{
				"numberOfResults": 236985,
				"displayName": "洋書",
				"classificationId": "52231011"
			},
			{
				"numberOfResults": 132804,
				"displayName": "家電&カメラ",
				"classificationId": "3210991"
			},
			{
				"numberOfResults": 53667,
				"displayName": "ファッション",
				"classificationId": "2229203051"
			},
			{
				"numberOfResults": 43870,
				"displayName": "ホーム&キッチン",
				"classificationId": "3839151"
			},
			{
				"numberOfResults": 39640,
				"displayName": "産業・研究開発用品",
				"classificationId": "3445394051"
			},
			{
				"numberOfResults": 31742,
				"displayName": "DIY・工具・ガーデン",
				"classificationId": "2016930051"
			},
			{
				"numberOfResults": 27062,
				"displayName": "車&バイク",
				"classificationId": "2017305051"
			},
			{
				"numberOfResults": 23699,
				"displayName": "DVD",
				"classificationId": "562002"
			},
			{
				"numberOfResults": 23013,
				"displayName": "パソコン・周辺機器",
				"classificationId": "2127210051"
			},
			{
				"numberOfResults": 18235,
				"displayName": "本",
				"classificationId": "465610"
			}
		]
	},
	"items": [
		{
			"asin": "1260470350",
			"images": [
				{
					"marketplaceId": "A1VC38T7YXB528",
					"images": [
						{
							"variant": "MAIN",
							"link": "https://m.media-amazon.com/images/I/41kSp18z0gL.jpg",
							"height": 500,
							"width": 389
						},
						{
							"variant": "PT01",
							"link": "https://m.media-amazon.com/images/I/41W8uD-hSoS.jpg",
							"height": 311,
							"width": 500
						},
						{
							"variant": "PT02",
							"link": "https://m.media-amazon.com/images/I/41RJrpym0US.jpg",
							"height": 309,
							"width": 500
						}
					]
				}
			],
			"summaries": [
				{
					"marketplaceId": "A1VC38T7YXB528",
					"brandName": "McGraw-Hill Education",
					"browseNode": "97801011",
					"itemName": "The Official Guide to the TOEFL iBT Test (Official Guide to the TOEFL Test)",
					"manufacturer": "McGraw-Hill"
				}
			]
		}
	]
}

getCatalogItems

from sp_api.api import CatalogItems
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
	#创建用于使用CatalogItems API的对象
	obj = CatalogItems(marketplace=Marketplaces.JP,
	                   credentials=event['credentials'])
	#设置检索所需的值并获取商品信息
	result = obj.get_catalog_item(asin='B09JFLJTZG')
	return result.payload

参数说明

asin:要获取详情的商品的ASIN

执行结果(示例)

{
	"asin": "B09JFLJTZG",
	"summaries": [
		{
			"marketplaceId": "A1VC38T7YXB528",
			"brandName": "Amazon",
			"browseNode": "2285132051",
			"colorName": "BLACK",
			"itemName": "Fire TV Stick 4K Max - Alexa対応音声認識リモコン(第3世代)付属 | ストリーミングメディアプレーヤー",
			"manufacturer": "Amazon",
			"modelNumber": "K2R2TE"
		}
	]
}

多设置几种参数,让自己能够获取所需的数据吧。

 
 
 

留言


© Copyright ROBIN planning LLC.

bottom of page