以下是淘宝API商品类目接口返回参数的详细解析,结合官方文档和实战案例为您说明:
一、核心参数详解
1. 类目基础信息
参数名 | 类型 | 说明 | 示例值 |
---|
cid | Integer | 类目唯一数字ID | 16(女装类目) |
name | String | 类目名称 | "连衣裙" |
parent_cid | Integer | 父类目ID(0表示根类目) | 16(若当前为子类目) |
is_leaf | Boolean | 是否为末级类目(无子类目) | true/false |
level | Integer | 类目层级(1-5级) | 3(三级类目) |
path | String | 类目路径(层级关系) | "16,18,26" |
2. 类目关系数据
参数名 | 类型 | 说明 | 示例值 |
---|
children | Array | 子类目列表(含ID和名称) | [{"cid":18,"name":"女装"}] |
is_root | Boolean | 是否为根类目 | false(非根类目) |
product_count | Integer | 类目下商品数量 | 12345 |
3. 扩展属性
参数名 | 类型 | 说明 | 示例值 |
---|
props | Object | 类目属性(如品牌、材质等) | {"品牌":["ZARA","H&M"]} |
status | String | 类目状态(normal/deleted) | "normal" |
sort_order | Integer | 类目排序权重 | 10 |
二、接口调用示例(Python)
python复制代码
| import requests |
| |
| def get_category_info(cid=0): |
| url = "https://gw.api.taobao.com/router/rest" |
| params = { |
| "method": "taobao.itemcats.get", |
| "app_key": "YOUR_APP_KEY", |
| "sign_method": "md5", |
| "timestamp": str(int(time.time())), |
| "format": "json", |
| "fields": "cid,name,parent_cid,children,product_count", |
| "parent_cid": cid |
| } |
| |
| # 生成签名(需自行实现签名算法) |
| params["sign"] = generate_sign(params, "YOUR_APP_SECRET") |
| |
| response = requests.get(url, params=params) |
| data = response.json() |
| return data["itemcats_get_response"]["item_cats"]["item_cat"] |
| |
| # 获取一级类目 |
| categories = get_category_info() |
| for cat in categories: |
| print(f"一级类目: {cat['name']} (ID:{cat['cid']})") |
| # 递归获取子类目 |
| sub_cats = get_category_info(cat['cid']) |
| for sub in sub_cats: |
| print(f" 子类目: {sub['name']} (商品数:{sub['product_count']})") |
三、典型应用场景
1. 商品发布系统
python复制代码
| def auto_categorize(product_title): |
| # 从根类目开始逐级匹配 |
| current_cats = get_category_info() |
| matched_cat = None |
| |
| for cat in current_cats: |
| if cat['name'].lower() in product_title.lower(): |
| # 优先匹配末级类目 |
| if cat['is_leaf']: |
| return cat |
| # 继续匹配子类目 |
| sub_cats = get_category_info(cat['cid']) |
| for sub in sub_cats: |
| if sub['name'].lower() in product_title.lower(): |
| return sub |
| return matched_cat |
2. 数据分析平台
sql复制代码
| SELECT |
| c.name AS category_name, |
| SUM(o.payment) AS gmv, |
| SUM(o.payment) / (SELECT SUM(payment) FROM orders) AS gmv_ratio |
| FROM orders o |
| JOIN products p ON o.item_id = p.id |
| JOIN categories c ON p.category_id = c.cid |
| WHERE c.level = 3 |
| GROUP BY c.name |
| ORDER BY gmv DESC; |
3. 智能导购系统
python复制代码
| def recommend_categories(user_history): |
| # 获取用户历史购买类目 |
| purchased_cats = set() |
| for item in user_history: |
| cat_info = get_category_info(item['category_id']) |
| purchased_cats.add(cat_info['parent_cid']) # 添加父类目 |
| |
| # 推荐未购买但相关的二级类目 |
| all_cats = get_category_info() |
| recommended = [] |
| for cat in all_cats: |
| if cat['level'] == 2 and cat['cid'] not in purchased_cats: |
| # 计算类目相似度(示例) |
| similarity = len(set(cat['props']['品牌']) & user_brands) / len(user_brands) |
| if similarity > 0.3: |
| recommended.append(cat) |
| return sorted(recommended, key=lambda x: x['product_count'], reverse=True)[:5] |
四、高级技巧
- 增量更新:
python复制代码
| # 缓存上次更新时间戳 |
| last_update = get_last_update_time() |
| |
| # 请求新增类目 |
| params["modified_start"] = last_update |
| new_cats = get_category_info() |
| |
| # 合并到本地数据库 |
| merge_categories(new_cats) |
- 多语言支持:
python复制代码
| def get_category_translations(cid): |
| params["lang"] = "en" # 支持cn/en/ru |
| response = requests.get(url, params=params) |
| return response.json()["translations"] |
- 性能优化:
- 使用Redis缓存高频类目数据
- 采用批量查询(最多50个cid/次)
- 设置合理请求间隔(建议QPS<30)
建议定期查阅淘宝开放平台/万邦开放平台,获取最新参数规范和接口升级信息。对于需要处理百万级类目的场景,可采用分布式任务队列(如Celery)进行并行抓取。