一、API认证准备
Access Key ID
Secret Access Key
合作伙伴标签(Associate Tag)
pip install boto3
二、核心请求示例
import boto3
from botocore.config import Config
配置认证信息
client = boto3.client(
'paapi5',
region_name='us-east-1',
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
config=Config(retries={'max_attempts': 3})
)
构造请求参数
request = {
"ItemIds": ["B08F7PBF5N"], # 商品ASIN
"Resources": [
"Images.Primary.Large",
"Offers.Listings.Price",
"ItemInfo.Title"
],
"PartnerTag": "your-associate-tag",
"PartnerType": "Associates"
}
发送请求
response = client.get_items(**request)
print(response['items'][0])
三、响应数据处理
关键字段解析:
item = response['items'][0]
商品标题
title = item['item_info']['title']['display_value']
价格
price = item['offers']['listings'][0]['price']['display_amount']
主图URL
image_url = item['images']['primary']['large']['url']
四、错误处理建议
from datetime import datetime
timestamp = datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
from botocore.exceptions import ClientError
try:
response = client.get_items(request)
except ClientError as e:
if e.response['Error']['Code'] == 'ThrottlingException':
time.sleep(2 attempt) # 指数退避
五、合规注意事项
严格遵守API使用条款
禁止缓存敏感数据(如实时价格)超过1小时
必须在展示页标注"来自亚马逊的API数据"
通过合理使用商品数据API,开发者可构建价格监控、选品分析等工具,但务必遵循平台规则以保障接口权限稳定。