一、接口概述
虾皮平台(Shopee)提供的关键词搜索API接口允许开发者通过指定搜索词获取商品列表数据。该接口采用RESTful设计,返回JSON格式数据,支持分页查询和多种筛选条件。
二、认证方式
调用接口需使用OAuth 2.0认证,需提前申请API Key和Secret Key:
import requests
API_KEY = "your_api_key"
SECRET_KEY = "your_secret_key"
三、请求参数
参数名 类型 必填 说明
keyword string 是 搜索关键词
page integer 否 分页页码(默认1)
limit integer 否 每页数量(默认20)
sort string 否 排序方式(price_asc/price_desc)
四、请求示例
def search_shopee(keyword, page=1):
url = "https://api.shopee.com/v2/search/items"
headers = {
"Authorization": f"Bearer {API_KEY}:{SECRET_KEY}"
}
params = {
"keyword": keyword,
"page": page,
"limit": 50
}
response = requests.get(url, headers=headers, params=params)
return response.json()
五、响应数据结构
{
"items": [
{
"item_id": 123456,
"title": "无线蓝牙耳机",
"price": 199.00,
"rating": 4.8,
"shop_location": "深圳"
}
],
"total_count": 1500,
"page_size": 50,
"has_next_page": true
}
六、错误处理
常见错误码:
400:请求参数错误
401:认证失败
429:请求频率超限
503:服务不可用
七、最佳实践
请求频率控制:建议不超过10次/秒
异常重试:使用指数退避算法
import time
def retry_search(keyword, retries=3):
for i in range(retries):
try:
return search_shopee(keyword)
except Exception:
time.sleep(2 ** i)
return None
八、注意事项
数据缓存周期建议≥1小时
商业用途需遵守平台API协议
敏感字段需脱敏处理