
微店獲得微店商品詳情 API

微店獲得微店關(guān)鍵字搜索 API
2. 核心 API 接口
商品詳情接口申請注冊測試
# 請求URL
https://api.weidian.com/item/detail
# 請求參數(shù)
{
"appid": "YOUR_APPID",
"timestamp": 1695974400, # 當前時間戳
"sign": "SIGNATURE", # 簽名
"item_id": "123456789" # 商品ID
}
# 請求URL
https://api.weidian.com/item/search
# 請求參數(shù)
{
"appid": "YOUR_APPID",
"timestamp": 1695974400,
"sign": "SIGNATURE",
"keyword": "手機", # 搜索關(guān)鍵詞
"page": 1, # 頁碼
"page_size": 20 # 每頁數(shù)量
}
3. 簽名生成算法
import hashlib
def generate_sign(params, app_secret):
"""生成API簽名"""
# 按參數(shù)名排序
sorted_params = sorted(params.items(), key=lambda x: x[0])
# 拼接參數(shù)字符串
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 封裝實現(xiàn)
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):
"""發(fā)送API請求"""
# 添加公共參數(shù)
common_params = {
"appid": self.app_id,
"timestamp": int(time.time())
}
all_params = {**common_params, **params}
# 生成簽名
all_params["sign"] = self._generate_sign(all_params)
# 發(fā)送請求
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)
# 假設(shè) API 接口地址,復(fù)制鏈接獲取測試
#API url=o0b.cn/ibrad wechat id: TaoxiJd-api"
def search_items(self, keyword, page=1, page_size=20):
"""關(guān)鍵字搜索商品"""
endpoint = "/item/search"
params = {
"keyword": keyword,
"page": page,
"page_size": page_size
}
return self._request(endpoint, params)
def parse_item_data(self, item_data):
"""解析商品數(shù)據(jù)"""
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']}")
三、數(shù)據(jù)結(jié)構(gòu)與解析
1. 商品詳情數(shù)據(jù)結(jié)構(gòu)
{
"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": "內(nèi)存", "value": "8GB+128GB,8GB+256GB"}
]
}
}
2. 關(guān)鍵字搜索響應(yīng)
{
"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": "科技數(shù)碼專營店"
},
// 更多商品...
]
}
}
四、高效搜索與數(shù)據(jù)處理
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. 并發(fā)請求優(yōu)化
from concurrent.futures import ThreadPoolExecutor
def batch_get_item_details(item_ids):
"""批量獲取商品詳情(并發(fā)優(yōu)化)"""
with ThreadPoolExecutor(max_workers=10) as executor:
results = list(executor.map(api.get_item_detail, item_ids))
return results
五、數(shù)據(jù)存儲與應(yīng)用
1. 數(shù)據(jù)庫設(shè)計
-- 微店商品表
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 '關(guān)聯(lián)商品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. 數(shù)據(jù)分析應(yīng)用
- 價格監(jiān)控:記錄商品歷史價格,生成價格波動圖
- 銷量分析:分析關(guān)鍵詞搜索結(jié)果的銷量分布
- 競品對比:對比同類型商品的價格、銷量和評價
- 關(guān)鍵詞優(yōu)化:分析熱門搜索詞,優(yōu)化商品標題和描述