前言
一、API接口概述
虾皮的商品详情API接口为开发者提供商品的完整信息,包括标题、价格、库存、描述、图片、规格参数等,支持电商导购、价格监控、库存同步等场景。以下是核心要点:
- 认证方式
- 需通过注册开发者账号,获取
Partner ID
和Secret Key
。 - 请求需生成签名(HMAC-SHA256),确保接口调用的安全性。
- 请求参数
- 必填参数:
partner_id
(合作伙伴ID)、item_id
(商品ID)、timestamp
(时间戳)、sign
(签名)。 - 可选参数:
shop_id
(店铺ID,用于指定店铺内的商品)。
- 响应格式
- 返回JSON数据,包含商品详情及错误码(如
error
字段)。
- 应用场景
- 电商导购:集成商品数据,提供个性化推荐。
- 价格监控:实时跟踪竞品价格变化。
- 库存管理:同步多平台库存,避免超卖。
- 数据分析:挖掘市场趋势,优化运营策略。
二、JSON数据示例
| { |
| "item": { |
| "item_id": 123456789, |
| "name": "iPhone 15 Pro Max 256GB 深空灰色", |
| "price": 899900, |
| "currency": "PHP", |
| "stock": 10, |
| "item_status": "normal", |
| "description": "全新正品,全国联保,支持分期付款...", |
| "images": [ |
| "https://cf.shopee.ph/file/123456789abc123/product-image.jpg" |
| ], |
| "shop": { |
| "shop_id": 12345, |
| "name": "官方旗舰店", |
| "rating": 4.9, |
| "follower_count": 150000 |
| }, |
| "ratings": { |
| "rating_star": 4.8, |
| "rating_count": 1500, |
| "rcount_with_image": 500 |
| }, |
| "categories": [ |
| { "cat_id": 123, "cat_name": "手机与配件" }, |
| { "cat_id": 456, "cat_name": "智能手机" } |
| ], |
| "attributes": [ |
| { "name": "品牌", "value": "Apple" }, |
| { "name": "型号", "value": "iPhone 15 Pro Max" } |
| ], |
| "models": [ |
| { |
| "model_id": 1, |
| "model_name": "256GB 深空灰色", |
| "price": 899900, |
| "stock": 10 |
| } |
| ] |
| }, |
| "error": null |
| } |
三、关键字段解析
字段名 | 类型 | 说明 |
---|
item_id | Int | 商品唯一标识 |
name | String | 商品名称 |
price | Float | 当前售价(单位由currency 决定) |
currency | String | 货币代码(如PHP、USD) |
stock | Int | 库存数量 |
images | Array | 商品图片URL列表 |
shop | Object | 店铺信息(含评分、粉丝数) |
ratings | Object | 用户评分数据 |
categories | Array | 商品所属分类(多级分类) |
attributes | Array | 商品属性(如品牌、型号) |
models | Array | 规格信息(如不同内存的价格) |
四、调用代码示例(Python)
| import requests |
| import hmac |
| import hashlib |
| import time |
| |
| def generate_signature(partner_id, secret_key, item_id, timestamp): |
| base_string = f"item_id={item_id}&partner_id={partner_id}×tamp={timestamp}" |
| signature = hmac.new( |
| secret_key.encode(), |
| base_string.encode(), |
| hashlib.sha256 |
| ).hexdigest() |
| return signature |
| |
| def get_item_detail(partner_id, secret_key, item_id): |
| timestamp = int(time.time()) |
| signature = generate_signature(partner_id, secret_key, item_id, timestamp) |
| url = "https://api.shopee.com/v2/item/get" |
| params = { |
| "partner_id": partner_id, |
| "item_id": item_id, |
| "timestamp": timestamp, |
| "sign": signature |
| } |
| response = requests.get(url, params=params) |
| return response.json() |
| |
| # 使用示例(需替换真实ID和密钥) |
| partner_id = "YOUR_PARTNER_ID" |
| secret_key = "YOUR_SECRET_KEY" |
| item_id = 123456789 |
| item_data = get_item_detail(partner_id, secret_key, item_id) |
| print(item_data) |
五、注意事项
- API密钥安全:避免将密钥硬编码在代码中,建议通过环境变量存储。
- 请求频率:遵守平台限流规则,防止触发反爬机制。
- 错误处理:检查HTTP状态码和返回数据中的
error
字段,确保健壮性。 - 分页支持:若商品数量较多,可通过
limit
和offset
参数分页获取数据。
通过上述接口和示例,开发者可高效获取结构化商品数据,为电商业务提供数据支持。