
给你极简、稳定、程序员专用的校验代码,用于 1688 商品详情 API 返回数据校验,确保数据准确、字段完整、格式合法,适合铺货、搬家、采购、数据分析。
def check_1688_item_accuracy(json_data):
"""
1688商品详情API 数据准确性校验
返回:(是否通过, 提示信息)
"""
try:
# 1. 校验返回根结构
if "result" not in json_data:
return False, "返回结构异常:无 result 节点"
result = json_data["result"]
if not result:
return False, "未获取到商品数据"
# 2. 商品ID校验(offerId 必须是数字)
offer_id = result.get("offerId")
if not offer_id or not str(offer_id).isdigit():
return False, f"商品ID无效:{offer_id}"
# 3. 标题校验(不能为空,长度合理)
title = result.get("subject")
if not title or len(title) < 5:
return False, "商品标题无效或过短"
# 4. 价格校验(必须 > 0)
price = result.get("price", "0")
try:
price_val = float(price)
if price_val <= 0:
return False, f"商品价格异常:{price}"
except:
return False, f"价格格式错误:{price}"
# 5. 主图校验(必须以 http/https 开头)
pic_url = result.get("imageUrl")
if not pic_url or "http" not in pic_url:
return False, "商品主图无效"
# 6. 库存/可售数量校验(不能为负)
stock = result.get("amount", 0)
try:
stock_val = int(stock)
if stock_val < 0:
return False, f"库存数量异常:{stock}"
except:
pass
# 7. 商家/公司信息校验
seller = result.get("supplierName")
if not seller:
return False, "商家信息缺失"
# 全部校验通过
return True, "1688商品数据校验通过,准确有效"
except Exception as e:
return False, f"校验异常:{str(e)}"json
{
"result": {
"offerId": "1234567890",
"subject": "2025新款纯棉短袖T恤工厂直供",
"price": "19.50",
"imageUrl": "https://cbu01.alicdn.com/xxx.jpg",
"amount": 5000,
"categoryId": 123456,
"supplierName": "XX服饰有限公司",
"detailUrl": "https://detail.1688.com/offer/xxx.html"
},
"code": 200,
"success": true
}# 调用1688 API 获取JSON
json_result = requests.get(api_url, params=params).json()
# 执行校验
is_ok, msg = check_1688_item_accuracy(json_result)
print(is_ok, msg)1688 商品 API 校验 = 结构正确 + 字段完整 + 数值合法
让你的商品搬家、自动铺货、采购比价、数据分析100% 稳定不出错。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。