
微店获得微店商品详情 API

微店获得微店关键字搜索 API
2. 核心 API 接口
商品详情接口申请注册测试
# 请求URL
https://api.weidian.com/item/detail
# 请求参数
{
"appid": "YOUR_APPID",
"timestamp": 1695974400, # 当前时间戳
"sign": "SIGNATURE", # 签名
"item_id": "123456789" # 商品ID
}
# 请求URL
https://api.weidian.com/item/search
# 请求参数
{
"appid": "YOUR_APPID",
"timestamp": 1695974400,
"sign": "SIGNATURE",
"keyword": "手机", # 搜索关键词
"page": 1, # 页码
"page_size": 20 # 每页数量
}
3. 签名生成算法
import hashlib
def generate_sign(params, app_secret):
"""生成API签名"""
# 按参数名排序
sorted_params = sorted(params.items(), key=lambda x: x[0])
# 拼接参数字符串
sign_str = ''.join([f"{k}{v}" for k, v in sorted_params])
# 拼接AppSecret
sign_str = app_secret + sign_str + app_secret
# MD5加密
return hashlib.md5(sign_str.encode('utf-8')).hexdigest().upper()
二、Python API 封装实现
import requests
import time
import json
import hashlib
class WeidianAPI:
def __init__(self, app_id, app_secret):
self.app_id = app_id
self.app_secret = app_secret
self.base_url = "https://api.weidian.com"
def _generate_sign(self, params):
"""生成API签名"""
sorted_params = sorted(params.items(), key=lambda x: x[0])
sign_str = ''.join([f"{k}{v}" for k, v in sorted_params])
sign_str = self.app_secret + sign_str + self.app_secret
return hashlib.md5(sign_str.encode('utf-8')).hexdigest().upper()
def _request(self, endpoint, params):
"""发送API请求"""
# 添加公共参数
common_params = {
"appid": self.app_id,
"timestamp": int(time.time())
}
all_params = {**common_params, **params}
# 生成签名
all_params["sign"] = self._generate_sign(all_params)
# 发送请求
url = f"{self.base_url}{endpoint}"
response = requests.post(url, json=all_params)
return response.json()
def get_item_detail(self, item_id):
"""获取商品详情"""
endpoint = "/item/detail"
params = {"item_id": item_id}
return self._request(endpoint, params)
# 假设 API 接口地址,复制链接获取测试
#API url=o0b.cn/ibrad wechat id: TaoxiJd-api"
def search_items(self, keyword, page=1, page_size=20):
"""关键字搜索商品"""
endpoint = "/item/search"
params = {
"keyword": keyword,
"page": page,
"page_size": page_size
}
return self._request(endpoint, params)
def parse_item_data(self, item_data):
"""解析商品数据"""
if not item_data:
return None
return {
"item_id": item_data.get("item_id"),
"title": item_data.get("title"),
"price": item_data.get("price"),
"original_price": item_data.get("original_price"),
"stock": item_data.get("stock"),
"sales": item_data.get("sales"),
"main_image": item_data.get("main_image"),
"detail_images": item_data.get("detail_images", []),
"category_id": item_data.get("category_id"),
"description": item_data.get("description")
}
# 使用示例
if __name__ == "__main__":
app_id = "YOUR_APPID"
app_secret = "YOUR_APPSECRET"
api = WeidianAPI(app_id, app_secret)
# 搜索商品
search_result = api.search_items("手机")
if search_result.get("code") == 0 and search_result.get("data"):
items = search_result["data"].get("items", [])
print(f"找到 {len(items)} 个商品")
if items:
# 获取第一个商品详情
first_item = items[0]
item_id = first_item["item_id"]
detail = api.get_item_detail(item_id)
if detail.get("code") == 0 and detail.get("data"):
parsed_item = api.parse_item_data(detail["data"])
print(f"商品标题: {parsed_item['title']}")
print(f"价格: {parsed_item['price']}")
print(f"销量: {parsed_item['sales']}")
三、数据结构与解析
1. 商品详情数据结构
{
"code": 0,
"message": "success",
"data": {
"item_id": "123456789",
"title": "2023新款智能手机",
"price": 2999.00,
"original_price": 3299.00,
"stock": 100,
"sales": 567,
"main_image": "https://img.weidian.com/item/123456.jpg",
"detail_images": [
"https://img.weidian.com/detail/123456_1.jpg",
"https://img.weidian.com/detail/123456_2.jpg"
],
"category_id": 1001,
"description": "这款手机拥有...",
"properties": [
{"name": "颜色", "value": "黑色,白色,蓝色"},
{"name": "内存", "value": "8GB+128GB,8GB+256GB"}
]
}
}
2. 关键字搜索响应
{
"code": 0,
"message": "success",
"data": {
"total": 1234,
"page": 1,
"page_size": 20,
"items": [
{
"item_id": "123456789",
"title": "2023新款智能手机",
"price": 2999.00,
"main_image": "https://img.weidian.com/item/123456.jpg",
"sales": 567,
"shop_id": "987654",
"shop_name": "科技数码专营店"
},
// 更多商品...
]
}
}
四、高效搜索与数据处理
1. 分页处理
def search_all_items(keyword):
"""搜索所有匹配的商品(处理分页)"""
all_items = []
page = 1
while True:
result = api.search_items(keyword, page=page)
if result.get("code") != 0 or not result.get("data"):
break
items = result["data"].get("items", [])
if not items:
break
all_items.extend(items)
# 判断是否还有下一页
total = result["data"].get("total", 0)
page_size = result["data"].get("page_size", 20)
if page * page_size >= total:
break
page += 1
return all_items
2. 并发请求优化
from concurrent.futures import ThreadPoolExecutor
def batch_get_item_details(item_ids):
"""批量获取商品详情(并发优化)"""
with ThreadPoolExecutor(max_workers=10) as executor:
results = list(executor.map(api.get_item_detail, item_ids))
return results
五、数据存储与应用
1. 数据库设计
-- 微店商品表
CREATE TABLE `weidian_items` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`item_id` varchar(32) NOT NULL COMMENT '微店商品ID',
`title` varchar(255) NOT NULL COMMENT '商品标题',
`price` decimal(10,2) NOT NULL COMMENT '价格',
`original_price` decimal(10,2) DEFAULT NULL COMMENT '原价',
`stock` int(11) DEFAULT 0 COMMENT '库存',
`sales` int(11) DEFAULT 0 COMMENT '销量',
`main_image` varchar(255) DEFAULT NULL COMMENT '主图URL',
`category_id` int(11) DEFAULT NULL COMMENT '分类ID',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_item_id` (`item_id`)
);
-- 商品详情图表
CREATE TABLE `weidian_item_images` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`item_id` varchar(32) NOT NULL COMMENT '关联商品ID',
`image_url` varchar(255) NOT NULL COMMENT '图片URL',
`type` tinyint(1) DEFAULT 0 COMMENT '0=详情图,1=主图',
PRIMARY KEY (`id`),
KEY `idx_item_id` (`item_id`)
);
2. 数据分析应用
- 价格监控:记录商品历史价格,生成价格波动图
- 销量分析:分析关键词搜索结果的销量分布
- 竞品对比:对比同类型商品的价格、销量和评价
- 关键词优化:分析热门搜索词,优化商品标题和描述