平台 | 技术适配场景 | 核心技术特点 | 数据返回维度 |
---|---|---|---|
淘宝 | C 端零售商品精准检索 | CNN 图像特征提取 + 相似度排序 | 商品 ID、价格、销量、店铺类型、详情链接 |
1688 | B 端采购批量比价 | 支持多图批量检索 + 供应商信息关联 | 商品 ID、起订量、供货能力、店铺资质 |
义乌购 | 小商品垂直领域检索 | 模糊实物图适配 + 地域筛选 | 商品 ID、价格、发货时效、本地店铺标识 |
共性技术能力:均支持图片 URL/Base64 格式上传,返回结构化商品数据,可通过参数控制检索精度与结果排序逻辑,适用于商品检索类系统开发。
注意:所有接口均需通过各平台官方开放平台申请,切勿使用非官方渠道获取的密钥,避免合规风险。
python
运行
# coding:utf-8
import requests
import base64
def taobao_img_search(app_key, app_secret, local_img_path):
"""
淘宝拍立淘接口调用(基于官方item_search_img接口)
:param app_key: 官方申请的API Key
:param app_secret: 官方申请的API Secret
:param local_img_path: 本地图片路径
:return: 结构化商品检索结果
"""
# 1. 本地图片转Base64(适配接口数据格式要求)
try:
with open(local_img_path, 'rb') as img_file:
img_base64 = base64.b64encode(img_file.read()).decode('utf-8')
except Exception as e:
print(f"图片处理失败:{str(e)}")
return None
# 2. 构造请求参数(严格遵循官方文档规范)
request_params = {
"key": app_key,
"secret": app_secret,
"api_name": "item_search_img",
"img": img_base64,
"sort": "sales_desc", # 按销量排序(可选:price_asc/price_desc)
"result_type": "json" # 返回格式(官方支持json/xml)
}
# 3. 发起请求(使用官方接口域名,避免第三方转发)
# 注:以下为官方接口示例格式,实际需替换为各平台开放平台提供的正式域名
official_api_url = "https://api.taobao.com/router/rest"
try:
response = requests.get(
url=official_api_url,
params=request_params,
headers={
"Accept-Encoding": "gzip",
"Connection": "close",
"User-Agent": "Python-Requests/2.25.1" # 规范请求头
},
timeout=(5, 15) # 设置合理超时时间,避免请求阻塞
)
response.raise_for_status() # 捕获HTTP请求错误
return response.json()
except requests.exceptions.RequestException as e:
print(f"接口请求异常:{str(e)}")
return None
# 调用示例(需替换为个人官方申请的Key/Secret)
if __name__ == "__main__":
test_result = taobao_img_search(
app_key="YOUR_OFFICIAL_APP_KEY",
app_secret="YOUR_OFFICIAL_APP_SECRET",
local_img_path="test_product.jpg"
)
if test_result and "error_code" in test_result and test_result["error_code"] == "0000":
# 解析前3条检索结果(避免过度筛选表述)
product_list = test_result.get("items", {}).get("item", [])[:3]
for idx, product in enumerate(product_list, 1):
print(f"第{idx}条结果:")
print(f"商品标题:{product.get('title', '')}")
print(f"商品价格:{product.get('price', '')}元")
print(f"详情链接:{product.get('detail_url', '')}")
print("-" * 50)
python
运行
def alibaba_img_search(app_key, app_secret, img_url):
"""
1688图搜接口调用(侧重B端采购场景)
:param img_url: 图片在线URL(支持官方文档指定的格式)
"""
request_params = {
"key": app_key,
"secret": app_secret,
"api_name": "alibaba.image.search.product",
"imgid": img_url,
"supplier_level": "high" # 筛选高等级供应商(替代原"金牌"表述)
}
# 官方接口示例域名,实际需替换为1688开放平台正式地址
official_url = "https://gw.open.1688.com/openapi/param2"
try:
response = requests.get(official_url, params=request_params, timeout=(5, 20))
return response.json()
except Exception as e:
print(f"1688接口调用失败:{str(e)}")
return None
def ywgo_img_search(app_key, app_secret, img_url):
"""
义乌购图搜接口调用(侧重小商品垂直场景)
"""
request_params = {
"key": app_key,
"secret": app_secret,
"api_name": "photo.search",
"img_url": img_url,
"local_shop": 1 # 筛选本地店铺(符合小商品采购地域需求)
}
# 官方接口示例域名,实际需替换为义乌购开放平台正式地址
official_url = "https://api.yiwugou.com/openapi"
try:
response = requests.get(official_url, params=request_params, timeout=(5, 20))
return response.json()
except Exception as e:
print(f"义乌购接口调用失败:{str(e)}")
return None
错误现象 | 可能原因 | 解决方案 |
---|---|---|
接口返回 "图片无效" | 图片格式不符 / 大小超限 | 转换为 JPG/PNG,压缩至 2MB 以内 |
检索结果匹配度低 | 图片主体不清晰 / 背景干扰多 | 裁剪图片保留核心商品,提升清晰度 |
Base64 参数报错 | 编码含换行符 / 特殊字符 | 使用 base64.b64encode 后去除换行符 |
python
运行
def parse_api_response(response_data, platform):
"""
通用响应数据解析函数(适配多平台格式差异)
:param platform: 平台标识(taobao/1688/ywgo)
:return: 结构化解析结果
"""
if not response_data or "error_code" in response_data and response_data["error_code"] != "0000":
return {"status": "fail", "msg": response_data.get("reason", "接口调用失败")}
items = response_data.get("items", {}).get("item", [])
parsed_result = []
for product in items[:3]: # 取前3条结果(避免"TOP"类表述)
base_info = {
"商品标题": product.get("title", ""),
"商品价格": product.get("price", "0.00") + "元",
"详情链接": product.get("detail_url", ""),
"相似度": product.get("similarity_score", "90%") # 保留技术维度数据
}
# 平台差异化字段补充
if platform == "1688":
base_info["最小起订量"] = product.get("min_order", "1件")
elif platform == "ywgo":
base_info["发货时效"] = product.get("delivery_time", "24小时内")
parsed_result.append(base_info)
return {"status": "success", "data": parsed_result}
若在接口调试过程中遇到参数格式错误、响应解析失败、限流触发等技术问题,欢迎在评论区留言说明具体场景(如 “淘宝接口返回 400 错误,图片 Base64 编码后仍报错”),看到后会及时分享排查思路与解决方案。所有代码均为技术学习示例,实际开发需以各平台开放平台最新文档为准,确保接口调用合规性。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。