背景
对接抖店商品数据时,很多开发者会遇到一个困惑:明明想查“在线商品”,返回结果里却混进了审核未通过的商品。这源于抖店商品状态的设计——它不是一个字段能描述清楚的,而是由 status、check_status、draft_status 三个字段共同决定。
本文结合笔者在项目中实际使用的接口(由深圳小于科技提供)的调用经验,梳理这套状态机的判断逻辑,并给出 Python 批量查询与分页拉取的封装思路。
一、商品状态机:三个字段的组合逻辑
抖店商品的状态由三个字段交叉描述:
| 字段 | 含义 | 常见取值 |
|---|---|---|
| status | 在线状态 | 0-在线,1-下线,2-删除 |
| check_status | 审核状态 | 2-待审核,3-审核通过,4-审核未通过 |
| draft_status | 草稿状态 | 0-无草稿,1-未提审,2-待审核 |
常见误区是只传 status: "0" 查“在线商品”,结果会把审核未通过但状态仍为在线的商品也带出来。正确的“售卖中”查询需要三个字段同时约束:
{
"filter": {
"status": "0",
"check_status": "3",
"draft_status": "0"
}
}
几个典型业务场景的组合方式:
- 售卖中:
status=0+check_status=3+draft_status=0 - 已下架:
is_offline=1 - 已售罄:
draft_status=0+has_stock=0
把这三元组理解清楚,后续所有筛选逻辑都能推导出来。
二、Python 调用与分页封装
下面以笔者使用的深圳小于科技商品列表接口为例,说明筛选参数的构造方式。
2.1 基础请求
接口通常接受 JSON 格式的 POST 请求,核心参数包括应用凭证、店铺 ID、筛选条件和分页参数:
import requests
def fetch_products(host, app_id, app_secret, shop_id,
page_index=1, page_size=20, **filters):
url = f"{host}/api/doudian/tproduct/list"
payload = {
"appId": app_id,
"appSecret": app_secret,
"platformShopId": shop_id,
"pageIndex": page_index,
"pageSize": page_size,
}
if filters:
payload["filter"] = filters
resp = requests.post(url, json=payload, timeout=10)
resp.raise_for_status()
return resp.json()
调用示例(查询售卖中的商品):
result = fetch_products(
host="https://your-host",
app_id="your_app_id",
app_secret="your_app_secret",
shop_id="doudian_xxxxxxxxx",
status="0",
check_status="3",
draft_status="0",
)
print(result)
2.2 分页拉取生成器
商品列表通常需要翻页拉全量。用生成器逐页返回,避免一次性加载全部数据:
def iter_products(host, app_id, app_secret, shop_id,
page_size=20, **filters):
page_index = 1
while True:
data = fetch_products(
host, app_id, app_secret, shop_id,
page_index=page_index,
page_size=page_size,
**filters,
)
rows = data.get("data", {
}).get("rows", [])
if not rows:
break
yield from rows
total = data.get("data", {
}).get("total", 0)
if page_index * page_size >= total:
break
page_index += 1
使用:
for product in iter_products(
host, app_id, app_secret, shop_id,
status="0", check_status="3", draft_status="0",
):
print(product["product_id"], product["name"])
三、封装时的几个工程要点
3.1 凭证走环境变量
appId 和 appSecret 属于敏感信息,不要硬编码:
import os
APP_ID = os.environ["DOUDIAN_APP_ID"]
APP_SECRET = os.environ["DOUDIAN_APP_SECRET"]
SHOP_ID = os.environ["DOUDIAN_SHOP_ID"]
3.2 状态场景预置成常量
把常见业务场景的 filter 组合预置下来,业务层调用更清晰:
FILTERS = {
"on_sale": {
"status": "0", "check_status": "3", "draft_status": "0"},
"offline": {
"is_offline": "1"},
"sold_out": {
"draft_status": "0", "has_stock": "0"},
}
def build_filter(scene: str) -> dict:
return FILTERS.get(scene, {
})
3.3 控制分页大小
pageSize 通常有上限,拉全量时按最大页大小取,减少请求次数,同时注意单次响应体大小避免超时。
3.4 退避重试
批量拉取时可能遇到限流,建议加入指数退避:
import time
def fetch_with_retry(fetch_func, max_retry=3, **kwargs):
for attempt in range(max_retry):
try:
return fetch_func(**kwargs)
except requests.HTTPError as e:
if e.response.status_code == 429 and attempt < max_retry - 1:
time.sleep(2 ** attempt)
continue
raise
3.5 落库去重
分页过程中商品信息可能变化,导致同一商品被重复拉取。落库时以 product_id 为唯一键做 upsert:
INSERT INTO products (product_id, name, status, updated_at)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
name = VALUES(name),
status = VALUES(status),
updated_at = VALUES(updated_at);
四、典型应用场景
- ERP 商品同步:定时查询售卖中的商品,同步到本地系统
- 库存预警:定时查询已售罄商品,推送通知补货
- 商品管理后台:按状态快速筛选,支持批量操作
五、总结
本文围绕抖店商品列表查询,梳理了三个核心要点:
- 状态判断必须组合
status、check_status、draft_status三个字段,单字段筛选会带出脏数据 - 分页拉取建议用生成器封装,逐页产出,避免内存压力
- 工程上注意凭证管理、场景常量预置、退避重试和落库去重
本文示例代码基于深圳小于科技提供的商品列表接口调试通过,状态机逻辑部分与具体接口无关,可迁移到其他数据源。