首页 > 大数据 > 数据分析 > 如何通过 API接口批量获取虾皮商品数据?
通过Shopee开放API批量获取商品数据需遵循以下步骤及技术实现方案:
GET https://api.shopee.com/api/v2/search_itemskeyword(关键词)、
limit(单页数量,默认20,最大100)、
page(页码)、
sort_by(排序方式,如
price_asc)、
country(地区后缀,如
.com.my)。https://api.shopee.com/api/v2/search_items?keyword=手机&limit=50&page=1&sort_by=sales&country=.com.myGET https://api.shopee.com/api/v2/product/getproduct_id(商品ID)、
shop_id(店铺ID)。GET https://api.shopee.com/api/v2/shop/itemsshop_id、
page、
limit。keyword=手机&limit=50),用API Key生成签名。Authorization: Bearer YOUR_ACCESS_TOKEN和
X-Shopee-Signature(签名)。pythonimport requestsimport timeimport hmacimport hashlibimport base64def generate_signature(params, api_key): message = "&".join([f"{k}={v}" for k, v in sorted(params.items())]) signature = hmac.new(api_key.encode(), message.encode(), hashlib.sha256).digest() return base64.b64encode(signature).decode()def search_items(keyword, limit=50, page=1, country=".com.my"): url = "https://api.shopee.com/api/v2/search_items" params = { "keyword": keyword, "limit": limit, "page": page, "sort_by": "sales", "country": country, "timestamp": int(time.time()) } headers = { "Authorization": f"Bearer YOUR_ACCESS_TOKEN", "X-Shopee-Signature": generate_signature(params, "YOUR_API_KEY") } response = requests.get(url, params=params, headers=headers) return response.json()# 批量获取并存储数据for page in range(1, 11): # 获取前10页 data = search_items("手机", page=page) with open("shopee_products.csv", "a", encoding="utf-8") as f: for item in data["items"]: f.write(f"{item['item_id']},{item['name']},{item['price']},{item['sales']}\n")
time.sleep(1)避免触发请求频率限制(如每分钟100次)。.com.my、越南
.vn)的接口参数和返回字段可能不同,需指定
country参数。通过以上步骤,可实现Shopee商品数据的批量获取,支撑电商分析、价格监控、竞品对比等场景需求。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70046834/viewspace-3102377/ ,如需转载,请注明出处,否则将追究法律责任。