微店 API 數據採集實例説明_開放平台

微店數據採集需基於微店開放平台提供的 API,需先完成開發者認證、創建應用並獲取密鑰,再遵循 API 規則調用接口。以下以Python為例,演示商品列表、訂單數據的採集流程(需提前完成開發者配置)。

一、準備工作

  1. 註冊開發者:訪問微店開放平台,完成企業 / 個人開發者認證,創建應用,獲取AppKeyAppSecret
  2. 獲取 Access Token:微店 API 採用 OAuth2.0 認證,需先通過AppKeyAppSecret獲取access_token(有效期通常為 2 小時)。
  3. API 文檔參考:查看微店開放平台的接口文檔(如商品接口、訂單接口),明確請求方式、參數、返回格式。

二、核心步驟與代碼實現

1. 獲取 Access Token

微店獲取 Token 的接口為:https://open.weidian.com/api/token(POST 請求)。

python

運行

import requests
import json

# 開發者配置(替換為自己的信息)
APP_KEY = "your_app_key"
APP_SECRET = "your_app_secret"
GRANT_TYPE = "client_credentials"  # 客户端模式

def get_access_token():
    url = "https://open.weidian.com/api/token"
    params = {
        "appkey": APP_KEY,
        "appsecret": APP_SECRET,
        "grant_type": GRANT_TYPE
    }
    try:
        response = requests.post(url, data=params)
        result = json.loads(response.text)
        if result.get("errcode") == 0:
            return result["access_token"]
        else:
            print(f"獲取Token失敗:{result['errmsg']}")
            return None
    except Exception as e:
        print(f"請求異常:{e}")
        return None

# 獲取Token
access_token = get_access_token()
if access_token:
    print(f"Access Token: {access_token}")

2. 採集商品列表數據

商品列表接口/v2/goods/list)為例,GET 請求,需傳入access_token、店鋪 ID 等參數。

python

運行

def get_goods_list(access_token, page=1, page_size=20):
    url = "https://open.weidian.com/api/v2/goods/list"
    params = {
        "access_token": access_token,
        "page": page,
        "page_size": page_size,
        "shop_id": "your_shop_id"  # 替換為店鋪ID
    }
    try:
        response = requests.get(url, params=params)
        result = json.loads(response.text)
        if result.get("errcode") == 0:
            goods_list = result["data"]["goods_list"]
            return goods_list
        else:
            print(f"獲取商品失敗:{result['errmsg']}")
            return []
    except Exception as e:
        print(f"請求異常:{e}")
        return []

# 獲取商品列表
goods_list = get_goods_list(access_token)
if goods_list:
    print(f"共獲取{len(goods_list)}件商品:")
    for goods in goods_list:
        print(f"商品ID:{goods['goods_id']},名稱:{goods['goods_name']},價格:{goods['price']}")

3. 採集訂單數據

訂單列表接口/v2/order/list)為例,GET 請求,需傳入時間範圍等參數。

python

運行

def get_order_list(access_token, start_time, end_time, page=1):
    url = "https://open.weidian.com/api/v2/order/list"
    params = {
        "access_token": access_token,
        "start_time": start_time,  # 開始時間(格式:yyyy-MM-dd HH:mm:ss)
        "end_time": end_time,      # 結束時間
        "page": page,
        "page_size": 20,
        "shop_id": "your_shop_id"
    }
    try:
        response = requests.get(url, params=params)
        result = json.loads(response.text)
        if result.get("errcode") == 0:
            order_list = result["data"]["order_list"]
            return order_list
        else:
            print(f"獲取訂單失敗:{result['errmsg']}")
            return []
    except Exception as e:
        print(f"請求異常:{e}")
        return []

# 獲取訂單列表(示例時間範圍)
order_list = get_order_list(access_token, "2025-12-01 00:00:00", "2025-12-03 23:59:59")
if order_list:
    print(f"共獲取{len(order_list)}條訂單:")
    for order in order_list:
        print(f"訂單號:{order['order_id']},金額:{order['total_amount']},狀態:{order['order_status']}")

三、注意事項

  1. 權限與合規:僅可採集自己店鋪的數據,需遵守《微店開放平台開發者協議》,禁止爬取他人數據。
  2. 接口限制:微店 API 有調用頻率限制(如單接口 QPS 限制),需控制請求頻率,避免被封禁。
  3. 簽名驗證:部分接口需進行簽名(如appsecret參與簽名),需嚴格按照文檔生成簽名參數。
  4. Token 刷新access_token過期後需重新獲取,建議在代碼中加入 Token 過期自動刷新邏輯。

四、數據存儲擴展

可將採集的數據保存到數據庫(如 MySQL)或文件(如 CSV):

python

運行

import csv

# 保存商品數據到CSV
with open("goods_list.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=["goods_id", "goods_name", "price"])
    writer.writeheader()
    writer.writerows(goods_list)

以上實例需根據微店開放平台的最新接口文檔調整參數和請求方式,確保與官方規則一致。