前言

我给你一套最简可运行代码,只需要替换 AppKey、AppSecret、AccessToken、店铺昵称 就能直接获取店铺所有商品(标题、价格、主图、销量、库存)。

一、你需要提前准备

  1. 淘宝开放平台创建应用 → 获取
    • app_key
    • app_secret
    • access_token(店铺授权)
  2. 目标店铺昵称(如:xx 官方旗舰店)

二、最简 Python 代码(直接复制运行)

python

运行

import requests
import time
import hashlib

# ===================== 【必须替换成你自己的】 =====================
APP_KEY = "你的AppKey"
APP_SECRET = "你的AppSecret"
ACCESS_TOKEN = "你的access_token"
SHOP_NICK = "店铺昵称"  # 例:"小米官方旗舰店"

# ===================== 淘宝API签名函数 =====================
def sign(params):
    sorted_params = sorted(params.items())
    param_str = "".join(f"{k}{v}" for k, v in sorted_params)
    sign_str = APP_SECRET + param_str + APP_SECRET
    return hashlib.md5(sign_str.encode()).hexdigest().upper()

# ===================== 获取店铺商品(一页最多200个) =====================
def get_taobao_shop_items(page_no=1):
    params = {
        "method": "taobao.items.onsale.get",  # 获取在售商品
        "app_key": APP_KEY,
        "session": ACCESS_TOKEN,
        "fields": "num_iid,title,price,pic_url,volume,sku",
        "page_no": page_no,
        "page_size": 200,
        "format": "json",
        "v": "2.0",
        "sign_method": "md5",
        "timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
    }
    
    params["sign"] = sign(params)
    url = "https://eco.taobao.com/router/rest"
    
    try:
        resp = requests.post(url, data=params, timeout=10)
        return resp.json()
    except:
        return {}

# ===================== 自动分页拉取【全店所有商品】 =====================
def get_all_shop_items():
    all_items = []
    page = 1
    
    while True:
        print(f"正在获取第 {page} 页...")
        data = get_taobao_shop_items(page)
        
        try:
            # 解析商品列表
            items = data["taobao_items_onsale_get_response"]["items"]["item"]
            if not items:
                break
            
            all_items.extend(items)
            page += 1
            time.sleep(1)  # 限流,必须加!
            
        except:
            break
    
    return all_items

# ===================== 运行 =====================
if __name__ == "__main__":
    items = get_all_shop_items()
    
    print(f"\n✅ 共获取商品数量:{len(items)}")
    
    # 输出前3个商品看看
    for i, item in enumerate(items[:3]):
        print(f"\n商品{i+1}:")
        print("  ID:", item["num_iid"])
        print("  标题:", item["title"])
        print("  价格:", item["price"])
        print("  主图:", item["pic_url"])

三、返回的商品字段(你能拿到什么)

plaintext

num_iid    商品ID
title      商品标题
price      价格
pic_url    主图
volume     销量
sku        规格库存

四、必须注意(防封 / 防报错)

  1. 必须加 sleep (1),1 秒 1 页,否则会被限流
  2. 一页最多 200 个商品
  3. 没有权限会报 401 / 403
  4. 店铺昵称必须正确

五、我可以帮你

需要我帮你:

  1. 把商品保存成 JSON / Excel
  2. 自动抓取 SKU、详情、描述
  3. 做成可视化界面
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐