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
  }
}
相关推荐
万邦科技Lafite21 小时前
利用淘宝商品详情接口获取商品价格,监控商品价格浮动
数据库·api·开放api接口·淘宝开放接口
有味道的男人2 天前
抖音商品详情api接口
api
曲幽2 天前
FastAPI单元测试实战:别等上线被喷才后悔,TestClient用对了真香!
python·单元测试·pytest·api·fastapi·web·httpx·testclient·依赖项覆盖
云蝠呼叫大模型联络中心2 天前
医疗智能客服系统架构设计与云蝠VoiceAgent API集成实践
人工智能·系统架构·api·医疗·voiceagent·ai 客服选型·智能客服 2026
Swift社区2 天前
Stage 模型改变的不是生命周期,而是系统边界
api·stage
深念Y2 天前
Docker Desktop 版本与 IDEA 插件兼容性
docker·云原生·容器·api·idea·wsl
用户528739463343 天前
AI 简历优化工具,一键润色,突出优势,提升求职通过率
api
笒鬼鬼4 天前
【API接口】最新可用红果短剧接口
算法·api·笒鬼鬼·红果短剧·接口源码
SuperherRo4 天前
API攻防-接口类型&测试方法&端点提取&暴漏攻击&枚举规则&RESTful风格&GraphQL语法
api·restful·graphql
147API4 天前
2026开发者实测:四大AI大模型API聚合网关SLA与延迟对决
api·api中转·api大模型·api大模型中转