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
  }
}
相关推荐
网安情报局17 小时前
告别排队与高延迟:直连GPT全系列,解锁低门槛、高稳定的AI生产力
人工智能·gpt·api·ai大模型
天空属于哈夫克32 天前
企微 RPA 接口开放:无需官方权限,外部群自由操作
自动化·企业微信·api
ZorChi2 天前
AI API 调用优化实战:统一入口与超时处理指南
人工智能·aigc·接口·api·agent·token·中转站
Resistance丶未来2 天前
管控用量,降本增效,MAI Gateway:助力企业搭建 Tokens 统一管理体系
人工智能·大模型·api·claude·ai安全·魔芋ai·maigateway
星浩AI3 天前
Agnes AI 免费 API 接入指南:文本、生图、生视频,一套接口全免费
llm·api·claude
触底反弹4 天前
大模型时代:5 个 Prompt 替代 BERT 训练,搞定 NLP 五大任务
人工智能·node.js·api
极连AI4 天前
国产大模型譬如DeepSeek接入codex教程分享
人工智能·gpt·chatgpt·api·token·极连ai·zovelox.com
MageGojo4 天前
OCR 火车票识别 API 服务介绍与使用考量
ocr·接口·api·数据提取·火车票识别
147API4 天前
Project Glasswing 扩展后,AI 安全扫描不能只看发现漏洞
人工智能·安全·api·claude
小二·5 天前
OpenAI API 实战指南
ai·openai·api