一、核心API接口与调用流程
-
接口选择
- 商品详情API :
jingdong.ware.get
或jd.item.detail.get
,通过商品ID(wareId/sku_id)获取完整属性。 - 商品搜索API :
jingdong.ware.search
,通过关键词获取商品列表及基础属性。 - SKU属性接口:直接查询SKU(如颜色、尺寸组合)的详细参数。
- 商品详情API :
-
调用步骤
-
注册与认证:在京东开放平台注册开发者账号,创建应用获取AppKey和AppSecret,申请商品详情API权限。
-
构造请求:
- 必填参数:商品ID(如
wareId=100060000001
)、字段列表(如field=title,price,attributes
)。 - 签名生成:按参数ASCII排序后拼接,首尾加AppSecret,MD5加密(部分接口用HMAC-SHA256)。
- 请求方式:支持GET/POST,URL示例:
https://api.jd.com/routerjson?method=jingdong.ware.get&app_key=XXX&wareId=100060000001...
- 必填参数:商品ID(如
-
响应解析 :返回JSON结构,核心字段位于
wareInfo
或sku_info
中。
-
二、属性字段解析
-
颜色(Color)
-
位置 :
attributes
数组或sku_info
的attributes
字段。 -
示例:
jsonjson "attributes": [ {"name": "颜色", "values": ["红色", "蓝色", "白色"]} ]
或
jsonjson "sku_info": [ {"sku_id": "100012345678", "attributes": "颜色:红色;尺寸:M", "price": "299.00"} ]
-
-
尺寸(Size)
-
位置 :同颜色,常与颜色组合在
attributes
中。 -
示例:
jsonjson "attributes": [ {"name": "尺寸", "values": ["S", "M", "L", "XL"]} ]
-
-
材质(Material)
-
位置 :
attributes
或商品描述字段(如desc
)。 -
示例:
jsonjson "attributes": [ {"name": "材质", "values": ["棉95%", "涤纶5%"]} ]
-
三、代码实现示例(Python)
python
python
import requests
import hashlib
import json
class JDAPI:
def __init__(self, app_key, app_secret):
self.app_key = app_key
self.app_secret = app_secret
self.api_url = "https://api.jd.com/routerjson"
def generate_sign(self, params):
sorted_params = sorted(params.items(), key=lambda x: x[0])
sign_str = self.app_secret + ''.join([f"{k}{v}" for k, v in sorted_params]) + self.app_secret
return hashlib.md5(sign_str.encode('utf-8')).hexdigest().upper()
def get_item_detail(self, ware_id):
params = {
"method": "jingdong.ware.get",
"app_key": self.app_key,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"format": "json",
"v": "2.0",
"wareId": ware_id,
"field": "title,price,attributes"
}
params["sign"] = self.generate_sign(params)
response = requests.get(self.api_url, params=params)
return response.json()
# 使用示例
if __name__ == "__main__":
app_key = "YOUR_APP_KEY"
app_secret = "YOUR_APP_SECRET"
ware_id = "100060000001"
jd_api = JDAPI(app_key, app_secret)
item_data = jd_api.get_item_detail(ware_id)
# 解析颜色、尺寸、材质
if "wareInfo" in item_data:
attributes = item_data["wareInfo"].get("attributes", [])
for attr in attributes:
if attr["name"] == "颜色":
colors = attr.get("values", [])
elif attr["name"] == "尺寸":
sizes = attr.get("values", [])
elif attr["name"] == "材质":
materials = attr.get("values", [])
print(f"颜色: {colors}, 尺寸: {sizes}, 材质: {materials}")
四、注意事项
-
权限与限制:
- 部分字段(如销量、评价)需企业认证或付费权限。
- 调用频率限制:默认100次/分钟,VIP账号可提升至500次/分钟。
- 需配置IP白名单,避免恶意调用。
-
数据结构差异:
- 不同商品类别属性结构可能不同(如服装vs电子产品),需动态解析。
- 多SKU商品需遍历
sku_info
数组获取各规格属性。
-
合规性:
- 禁止爬虫抓取,必须使用官方API。
- 敏感信息需脱敏处理,遵循京东数据使用规范。
通过以上流程,可高效获取并解析京东商品的颜色、尺寸和材质属性,适用于电商数据分析、竞品监控等场景。建议定期查阅京东开放平台最新文档,确保接口调用符合规范。