首页 > 大数据 > 数据分析 > 微店根据关键词批量获取商品信息API接口操作指南
AppKey和
AppSecret(接口调用身份凭证)。access_token(有效期2小时,需定期刷新):
pythonimport requestsauth_url = "https://open.weidian.com/api/oauth2/token"payload = { "grant_type": "client_credentials", "client_id": "YOUR_CLIENT_ID", # 替换为AppKey "client_secret": "YOUR_CLIENT_SECRET" # 替换为AppSecret}response = requests.post(auth_url, data=payload)access_token = response.json()["access_token"]https://api.weidian.com/item/search(正式环境)app_key:开发者AppKeykeyword:搜索关键词(如“女装”)page_no:分页页码page_size:每页数量(默认20,最大100)sort_type:排序方式(1=综合排序,2=销量优先,3=价格升序,4=价格降序)start_price/
end_price:价格区间筛选pythonimport hmacimport urllib.parsefrom hashlib import sha256def generate_sign(params, app_secret): sorted_params = sorted(params.items(), key=lambda x: x[0]) query_str = "&".join([f"{k}={urllib.parse.quote_plus(str(v))}" for k, v in sorted_params]) sign = hmac.new(app_secret.encode(), query_str.encode(), sha256).hexdigest().upper() return signhttps://api.weidian.com/v1/product/getproduct_id:商品IDaccess_token:授权凭证pythonimport requestsurl = "https://api.weidian.com/v1/product/get"params = { "product_id": "12345678", "access_token": access_token}headers = {"Authorization": f"Bearer {access_token}"}response = requests.get(url, headers=headers, params=params)product_data = response.json()["data"]item.search获取总页数
total_page,循环遍历所有分页:
pythontotal_count = response.json()["data"]["total_count"]total_page = (total_count + 19) // 20 # 每页20条for page in range(1, total_page + 1): search_products(keyword, page=page, size=20)
aiohttp)控制并发量:
pythonimport asyncioimport aiohttpasync def fetch_items(session, item_id, access_token): url = f"https://api.weidian.com/micro/item_get?accessToken={access_token}&num_iid={item_id}" async with session.get(url) as response: return await response.json()async def main(): item_ids = [123, 456, 789] async with aiohttp.ClientSession() as session: tasks = [fetch_items(session, id, access_token) for id in item_ids] results = await asyncio.gather(*tasks)asyncio.run(main())update_time,标记“新增/修改/下架”状态。通过以上步骤,可实现高效、合规的微店商品信息批量获取。如遇问题,可联系微店开放平台客服获取支持。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70046834/viewspace-3101044/ ,如需转载,请注明出处,否则将追究法律责任。