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
  }
}
相关推荐
API开发2 天前
apiSQL网关 for Docker 离线安装和升级教程
运维·docker·容器·api·api网关·apisql·替代graphql
新诺韦尔API3 天前
手机三要素验证接口详细技术对接指南
大数据·智能手机·api
没有bug.的程序员3 天前
Spring Boot 与 Swagger:API 文档自动化生成、版本管理与云原生协作深度实战指南
spring boot·云原生·自动化·api·swagger·版本管理·自动化生产
Li emily5 天前
如何通过外汇API平台快速实现实时数据接入?
开发语言·python·api·fastapi·美股
Onelafite7 天前
京东商品属性的详细api数据解析:颜色、尺寸与材质
linux·数据库·api·材质·开放api
nangonghen7 天前
centos 7.9安装postman v9.31.0
centos·api·postman
新诺韦尔API7 天前
手机三要素验证接口接入常见问题一览
大数据·智能手机·api
霑潇雨8 天前
Flink的转换算子——map
大数据·开发语言·flink·api
爱喝水的鱼丶11 天前
SAP-ABAP:从SAP中暴露REST API:完整实操SICF接口开发指南
运维·开发语言·api·sap·abap·rest·接口开发
曲幽11 天前
FastAPI异常处理全解析:别让你的API在用户面前“裸奔”
python·websocket·api·fastapi·web·exception·error·httexception