Python采集淘宝商品详情API接口

淘宝官方并没有公开的商品详情API接口,但可以通过以下几种方式获取商品数据:

方法一:使用淘宝开放平台API(需要申请)

淘宝开放平台提供了合法的API接口,需要申请成为开发者并创建应用。

python 复制代码
python
import requests
import json
 
def get_taobao_item_detail(app_key, app_secret, num_iid):
    """
    通过淘宝开放平台API获取商品详情
    需要先申请成为淘宝开放平台开发者
    """
    url = "http://gw.api.taobao.com/router/rest"
    
    params = {
        "method": "taobao.item.get",
        "app_key": app_key,
        "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "format": "json",
        "v": "2.0",
        "sign_method": "md5",
        "num_iid": num_iid,
        "fields": "detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual"
    }
    
    # 生成签名(这里简化了,实际需要按照淘宝签名规则生成)
    import hashlib
    sign_str = app_secret + ''.join([k + v for k, v in sorted(params.items())]) + app_secret
    sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest().upper()
    params['sign'] = sign
    
    response = requests.get(url, params=params)
    return response.json()
 
# 使用示例
# result = get_taobao_item_detail("your_app_key", "your_app_secret", "商品ID")
# print(json.dumps(result, indent=2, ensure_ascii=False))

方法二:模拟网页请求(非官方,可能有风险)

淘宝网页版的数据可以通过分析接口获取,但需要注意:

  1. 可能违反淘宝的使用条款
  2. 接口可能随时变更
  3. 需要处理反爬机制
python 复制代码
python
import requests
import json
from urllib.parse import quote
 
def get_taobao_item_detail_web(item_id):
    """
    通过模拟网页请求获取商品详情(非官方方式)
    """
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'Referer': f'https://item.taobao.com/item.htm?id={item_id}'
    }
    
    # 淘宝商品详情API接口(可能需要更新)
    api_url = f'https://h5api.m.taobao.com/h5/mtop.taobao.detail.getdetail/6.0/?data=%7B%22itemNumId%22%3A%22{item_id}%22%7D'
    
    response = requests.get(api_url, headers=headers)
    data = response.json()
    
    if 'data' in data:
        return data['data']
    else:
        return None
 
# 使用示例
# item_id = "123456789"  # 替换为实际商品ID
# detail = get_taobao_item_detail_web(item_id)
# print(json.dumps(detail, indent=2, ensure_ascii=False))

方法三:使用第三方数据服务

有一些第三方服务提供了淘宝商品数据采集API,如:

  • 淘宝客API
  • 某些数据采集平台
python 复制代码
python
def get_taobao_item_thirdparty(api_key, item_id):
    """
    通过第三方服务获取淘宝商品数据
    """
    url = f"https://api.example.com/taobao/item?id={item_id}&apikey={api_key}"
    response = requests.get(url)
    return response.json()

返回的JSON数据结构示例

以下是淘宝商品详情可能包含的字段(实际返回可能有所不同): API接口测试

json 复制代码
json
{
  "item": {
    "numIid": "123456789",
    "title": "商品标题",
    "price": "99.00",
    "originalPrice": "199.00",
    "sold": 1000,
    "stock": 500,
    "images": [
      "https://img.alicdn.com/imgextra/i1/123456789/O1CN01abc123_123456789.jpg",
      "https://img.alicdn.com/imgextra/i2/123456789/O1CN01def456_123456789.jpg"
    ],
    "desc": "商品描述HTML",
    "props": [
      {"name": "颜色", "value": "红色"},
      {"name": "尺寸", "value": "XL"}
    ],
    "shop": {
      "id": "987654321",
      "name": "店铺名称",
      "score": 4.8,
      "url": "https://shop.taobao.com/987654321.html"
    },
    "sku": {
      "prices": {
        "红色-XL": "99.00",
        "蓝色-L": "89.00"
      },
      "stocks": {
        "红色-XL": 100,
        "蓝色-L": 50
      }
    }
  },
  "apiInfo": {
    "status": "success",
    "message": "",
    "timestamp": 1634567890
  }
}
相关推荐
POLOAPI2 小时前
藏在 Anthropic API 里的秘密武器:Claude Code 让你的密钥价值翻倍
人工智能·api·ai编程
蓝倾21 小时前
批量获取亚马逊商品SKU商品规格调用流程
api·fastapi
拾光拾趣录1 天前
画中画还能这么玩?用 Document Picture-in-Picture API 打造沉浸式多任务体验
前端·api
DM今天肝到几点?1 天前
时隔六年!OpenAI 首发 GPT-OSS 120B / 20B 开源模型:性能、安全与授权细节全解
vscode·gpt·ai·chatgpt·大模型·api·claude
用户268001379191 天前
微店商品详情API接口系列,API接口请求如下
api
崔庆才丨静觅2 天前
Hailuo Videos Generation API 对接说明
api
崔庆才丨静觅2 天前
Veo Videos Generation API 对接说明
人工智能·api
电商api24677428103 天前
微信小程序API+京东支付,无缝结账体验满分!
api
POLOAPI3 天前
告别敲代码?Claude Code 让命令行自己 “写指令”,AI 正在重构程序员的双手
人工智能·api