智慧园区平台完整方案
2026/5/14 18:56:07
当当item_get(官方标准名称为dangdang.item.get)是通过商品 ID 或 ISBN 获取图书、百货等商品全量结构化数据的核心接口,覆盖标题、价格、库存、属性、多媒体、销售与售后等字段,适配商品展示、价格监控、竞品分析、库存管理等场景open.dangdang.com。该接口采用HTTPS+AppKey/AppSecret 签名认证,支持 JSON/XML 返回,具备数据权威、字段完整、权限分级严格的特点。本攻略从接口认知、权限获取、实操对接、调试排错到生产级优化,提供全链路结构化指导,兼顾入门易用性与企业级稳定性。
| 参数类型 | 参数名称 | 类型 | 是否必填 | 说明 | 应用示例 |
|---|---|---|---|---|---|
| 公共参数 | method | string | 是 | 接口名称,固定为dangdang.item.get | dangdang.item.get |
| app_key | string | 是 | 开放平台应用唯一标识 | 12345678 | |
| timestamp | string | 是 | 秒级时间戳(10 位) | 1735689600 | |
| format | string | 否 | 响应格式,支持 json/xml,默认 json | json | |
| sign | string | 是 | 签名(MD5 加密,生成规则见下文) | e10adc3949ba59abbe56e057f20f883e | |
| sign_method | string | 否 | 签名算法,固定为md5 | md5 | |
| 业务参数 | product_id | string | 二选一 | 商品 ID(当当内部唯一标识) | 21068943 |
| isbn | string | 二选一 | 国际标准书号(支持 13 位 / 10 位) | 9787108009821 | |
| need_stock | bool | 否 | 是否返回库存数据(true = 是,false = 否,默认 false) | true | |
| detail_level | int | 否 | 详情级别(1 = 基础,2 = 扩展,3 = 完整,默认 1) | 3 |
注意事项
product_id与isbn二选一,优先使用product_id(图书以外商品无 ISBN);- 签名生成需将所有非空参数按 ASCII 升序排序,拼接 AppSecret 后 MD5 加密,确保参数无遗漏;
- 接口支持 GET/POST 请求,GET 参数拼接在 URL 中,POST 参数放在请求体,签名需包含所有参数。
| 字段分类 | 核心字段 | 说明 |
|---|---|---|
| 基础商品信息 | product_id | 商品唯一 ID |
| isbn | 国际标准书号(图书类) | |
| title | 商品主标题 | |
| sub_title | 商品副标题 | |
| brand | 品牌名称 | |
| author | 作者(图书类) | |
| press | 出版社(图书类) | |
| publish_time | 上架 / 出版时间 | |
| category | 所属分类(多级) | |
| 价格信息 | current_price | 当前售价 |
| original_price | 原价 | |
| discount | 折扣(current_price/original_price) | |
| member_price | 会员价(需高级权限) | |
| 库存与物流 | stock | 实时库存量 |
| region_stock | 区域仓库存分布(需高级权限) | |
| delivery_type | 配送方式(快递 / 自提等) | |
| 多媒体数据 | cover_url | 封面图 URL |
| detail_urls | 详情图 URL 列表 | |
| video_url | 商品视频链接 | |
| 销售数据 | sales_volume | 累计销量 |
| score | 商品评分 | |
| comment_count | 评论数 | |
| 商品属性 | page_count | 页数(图书类) |
| format | 开本(图书类) | |
| weight | 重量(百货类) | |
| 售后信息 | return_policy | 退货政策 |
| warranty | 质保期 |
提示:
detail_level=3会返回完整商品描述、属性等大字段,响应体积较大,非必要不开启,避免影响接口性能。
当当item_get接口由当当开放平台提供,接入步骤如下:
AppKey和AppSecret(接口调用核心凭证),配置 IP 白名单;dangdang.item.get接口权限,根据业务需求选择权限等级(基础 / 进阶 / 高级)。风险提示:严禁使用非合规爬虫、第三方代理接口抓取数据,违反平台协议,会导致账号封禁、法律追责。
| 工具类型 | 推荐工具 | 用途 |
|---|---|---|
| 调试工具 | 当当开放平台调试工具 | 自动生成签名,验证参数与响应结果 |
| Postman | 模拟 GET/POST 请求,排查代码逻辑问题 | |
| 时间戳生成器 | 获取秒级时间戳,确保签名参数正确 | |
| 开发依赖 | requests | 发送 HTTPS 请求 |
| hashlib | 生成 MD5 签名,确保接口安全 | |
| jsonpath-ng | 快速解析嵌套 JSON 响应数据 | |
| pandas | 批量整理商品详情数据,生成 Excel 报告 | |
| 辅助工具 | Redis | 缓存商品详情,减少接口调用次数 |
| logging | 记录接口调用日志,便于审计与问题追溯 |
当当接口采用AppKey+AppSecret 签名认证机制,签名生成步骤如下:
key1value1key2value2...格式;bash
运行
pip install requests hashlib jsonpath-ng pandasimport requests import hashlib import time import logging import pandas as pd from urllib.parse import urlencode from jsonpath_ng import parse # 封装好API供应商demo url=https://console.open.onebound.cn/console/?i=Lex # 日志配置 logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s", handlers=[logging.FileHandler("dangdang_item_get.log"), logging.StreamHandler()] ) # 配置信息(替换为你的当当开放平台信息) CONFIG = { "app_key": "你的AppKey", "app_secret": "你的AppSecret", "api_url": "https://api.dangdang.com/router", "format": "json", "sign_method": "md5" } def generate_sign(params: dict, app_secret: str) -> str: """生成当当接口签名(MD5加密)""" # 1. 按参数名ASCII升序排序 sorted_params = sorted(params.items(), key=lambda x: x[0]) # 2. 拼接参数为key1value1key2value2...格式 param_str = "".join([f"{k}{v}" for k, v in sorted_params]) # 3. 拼接AppSecret sign_str = param_str + app_secret # 4. MD5加密,转为小写 sign = hashlib.md5(sign_str.encode("utf-8")).hexdigest().lower() return sign def standardize_product_data(raw_product: dict) -> dict: """标准化当当商品详情数据,统一输出格式""" base_info = raw_product.get("base_info", {}) price_info = raw_product.get("price_info", {}) stock_info = raw_product.get("stock_info", {}) sales_info = raw_product.get("sales_info", {}) media_info = raw_product.get("media_info", {}) attr_info = raw_product.get("attr_info", {}) return { "商品ID": base_info.get("product_id", ""), "ISBN": base_info.get("isbn", ""), "商品标题": base_info.get("title", ""), "副标题": base_info.get("sub_title", ""), "品牌": base_info.get("brand", ""), "作者": base_info.get("author", ""), "出版社": base_info.get("press", ""), "上架时间": base_info.get("publish_time", ""), "所属分类": "/".join(base_info.get("category", [])), "当前售价": price_info.get("current_price", 0.0), "原价": price_info.get("original_price", 0.0), "折扣": round(price_info.get("current_price", 0.0) / price_info.get("original_price", 1.0), 2) if price_info.get("original_price", 0) != 0 else 0.0, "库存": stock_info.get("stock", 0), "销量": sales_info.get("sales_volume", 0), "评分": sales_info.get("score", 0.0), "评论数": sales_info.get("comment_count", 0), "封面图URL": media_info.get("cover_url", ""), "详情图URL列表": ",".join(media_info.get("detail_urls", [])), "页数": attr_info.get("page_count", 0), "开本": attr_info.get("format", ""), "请求时间": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) } def dangdang_item_get( product_id: str = None, isbn: str = None, need_stock: bool = False, detail_level: int = 1 ) -> dict: """调用当当item_get接口获取商品详情""" # 1. 校验必填参数(product_id与isbn二选一) if not product_id and not isbn: return {"success": False, "error_msg": "product_id与isbn必须填写一个", "data": {}} # 2. 构建公共参数 timestamp = str(int(time.time())) params = { "method": "dangdang.item.get", "app_key": CONFIG["app_key"], "timestamp": timestamp, "format": CONFIG["format"], "sign_method": CONFIG["sign_method"], "detail_level": detail_level, "need_stock": str(need_stock).lower() } # 3. 补充业务参数 if product_id: params["product_id"] = product_id if isbn: params["isbn"] = isbn # 4. 生成签名 sign = generate_sign(params, CONFIG["app_secret"]) params["sign"] = sign try: # 5. 发送GET请求 response = requests.get( url=CONFIG["api_url"], params=params, timeout=10, verify=True ) response.raise_for_status() result = response.json() # 6. 解析响应结果 if result.get("error_response"): error = result["error_response"] error_msg = f"{error.get('code', '')}: {error.get('msg', '')}" logging.error(f"接口调用失败:{error_msg}") return {"success": False, "error_msg": error_msg, "data": {}} raw_product = result.get("item_get_response", {}).get("product", {}) if not raw_product: logging.warning(f"无商品数据返回(商品ID:{product_id},ISBN:{isbn})") return {"success": False, "error_msg": "无商品数据", "data": {}} # 7. 标准化数据 standard_data = standardize_product_data(raw_product) return { "success": True, "data": standard_data, "error_msg": "" } except requests.exceptions.RequestException as e: logging.error(f"网络请求异常:{str(e)}") return {"success": False, "error_msg": f"网络异常:{str(e)}", "data": {}} except Exception as e: logging.error(f"数据解析异常:{str(e)}") return {"success": False, "error_msg": f"解析异常:{str(e)}", "data": {}} # 封装好API供应商demo url=https://console.open.onebound.cn/console/?i=Lex # 调用示例 if __name__ == "__main__": product_id = "21068943" # 替换为目标商品ID need_stock = True detail_level = 2 result = dangdang_item_get( product_id=product_id, need_stock=need_stock, detail_level=detail_level ) if result["success"]: print("当当商品详情:") for k, v in result["data"].items(): print(f"{k}: {v}") # 保存为Excel df = pd.DataFrame([result["data"]]) df.to_excel(f"dangdang_product_detail_{product_id}.xlsx", index=False) else: print(f"获取失败:{result['error_msg']}")dangdang.item.get接口;| 问题现象 | 常见原因 | 解决方案 |
|---|---|---|
| 签名验证失败(401) | 1. AppKey/AppSecret 错误;2. 签名生成规则错误;3. timestamp 过期(超过 5 分钟) | 1. 核对开放平台应用信息;2. 严格按 ASCII 升序排序参数,拼接 AppSecret 后 MD5 加密;3. 实时生成 timestamp,确保请求在 5 分钟内发送 |
| 权限不足(403) | 1. 未申请 dangdang.item.get 接口权限;2. IP 不在白名单;3. 权限等级不足(如请求会员价) | 1. 在开放平台申请对应权限;2. 添加服务器 IP 到白名单;3. 升级权限等级,申请敏感数据访问权限 |
| 参数错误(400) | 1. product_id 与 isbn 均为空;2. detail_level 值非法(非 1/2/3);3. need_stock 格式错误(非 bool) | 1. 确保二选一参数非空;2. detail_level 设置为 1/2/3;3. need_stock 转为小写字符串(true/false) |
| 无商品数据返回 | 1. 商品 ID/ISBN 错误;2. 商品已下架 / 删除;3. 商品为敏感类型(如禁售品) | 1. 核对商品 ID/ISBN 是否准确;2. 在当当官网搜索商品,确认商品状态;3. 调整商品类型,选择合规商品 |
| 响应超时(504) | 1. 网络波动;2. detail_level=3 导致响应体积过大;3. 高峰期调用 | 1. 添加重试机制;2. 降低 detail_level(如改为 1);3. 避开高峰期(如工作日 10:00-12:00) |
aiohttp),控制并发数≤权限允许的频率上限(如企业基础权限 5 次 / 秒);dangdang_product_商品ID_need_stock_detail_level,动态数据(价格、库存)缓存 5 分钟,基础信息缓存 15 分钟,减少重复调用;item_search获取商品 ID 列表,批量调用item_get获取商品详情,实现 “搜索 - 详情” 全链路数据采集;item_get监控目标商品价格,当售价低于阈值时触发热门告警,及时调整采购策略;