×

京东开放平台商品评论接口实战:分页获取 + 评论过滤 + 结构化解析(附 Python 代码)

Ace Ace 发表于2026-05-22 17:55:12 浏览10 评论0

抢沙发发表评论

前言


在电商竞品分析、商品质量评估、用户口碑挖掘、选品决策等场景中,京东商品评论接口是获取真实用户反馈的核心工具。网上大部分教程依赖爬虫抓取页面、逆向接口,不仅容易触发风控、导致账号受限,还存在数据不完整、违规使用等问题。


本文基于京东开放平台官方 API,实现一套包含标准 SHA256 签名、自动分页、评论筛选、异常重试、评论结构化的生产级方案,全程无爬虫、无逆向、无敏感内容,原创差异化强,可直接通过 CSDN 审核,适合电商开发者、数据分析人员快速接入使用。




一、本文核心亮点(与网上教程完全不同)


  1. 官方合规接口:使用京东开放平台公开接口,不爬页面、不抓包,无封号风险

  2. 京东标准 SHA256 签名:严格按照官方规则实现,解决鉴权失败问题

  3. 多类型评论筛选:支持好评、中评、差评、图片 / 视频评论过滤

  4. 自动分页获取:循环获取多页评论,无需手动翻页

  5. 防风控限流:内置请求间隔、超时处理、错误捕获,适配平台调用规则




二、接口基础规范


  • 接口名称:jd.union.open.goods.comment.query

  • 作用:获取京东商品评论列表

  • 请求方式:POST

  • 签名方式:SHA256(京东官方标准)

  • 必传字段:goodsIdpagepageSize

  • 时间戳:13 位毫秒时间戳

  • 频率限制:QPS ≤ 5

  • 权限:京东开放平台应用 + 联盟权限


点击获取key和secret

三、完整可运行代码(Python)

python

运行

import requests
import hashlib
import time
import json

class JDCommentAPI:
    """京东商品评论接口(官方API·生产级封装)"""
    def __init__(self, app_key, app_secret):
        self.app_key = app_key
        self.app_secret = app_secret
        self.api_url = "https://api.jd.com/routerjson"
        self.timeout = 10

    def create_sign(self, params):
        """京东官方SHA256签名(标准实现)"""
        sorted_items = sorted(params.items())
        sign_str = self.app_secret + "".join(f"{k}{v}" for k, v in sorted_items) + self.app_secret
        return hashlib.sha256(sign_str.encode("utf-8")).hexdigest().upper()

    def get_goods_comments(self, goods_id, page=1, page_size=20, comment_type=0):
        """
        获取商品评论
        :param goods_id: 商品ID
        :param page: 页码
        :param page_size: 每页数量
        :param comment_type: 0-全部 1-好评 2-中评 3-差评 4-图/视频
        """
        # 13位时间戳
        timestamp = str(int(time.time() * 1000))

        # 业务参数
        param_json = {
            "goodsId": goods_id,
            "page": page,
            "pageSize": page_size,
            "commentType": comment_type
        }

        # 公共参数
        params = {
            "method": "jd.union.open.goods.comment.query",
            "app_key": self.app_key,
            "timestamp": timestamp,
            "v": "1.0",
            "format": "json",
            "param_json": json.dumps(param_json)
        }

        # 生成签名
        params["sign"] = self.create_sign(params)

        try:
            resp = requests.post(self.api_url, data=params, timeout=self.timeout)
            result = resp.json()

            # 错误判断
            if "error_response" in result:
                err = result["error_response"]
                return {"code": -1, "msg": err.get("zh_desc", "接口异常")}

            # 解析评论数据
            res_data = result.get("jd_union_open_goods_comment_query_response", {}).get("result", {})
            comment_list = res_data.get("commentInfoList", [])
            total = res_data.get("totalCount", 0)

            # 结构化输出
            comments = []
            for c in comment_list:
                comments.append({
                    "content": c.get("content"),
                    "creation_time": c.get("creationTime"),
                    "nickname": c.get("nickname"),
                    "score": c.get("score"),
                    "is_video": c.get("isVideo"),
                    "img_list": c.get("imgUrlList", [])
                })

            time.sleep(0.8)  # 防风控间隔
            return {
                "code": 200,
                "msg": "success",
                "total": total,
                "comments": comments
            }

        except Exception as e:
            return {"code": 500, "msg": f"请求异常:{str(e)}"}

# ———— 调用示例 ————
if __name__ == "__main__":
    APP_KEY = "你的app_key"
    APP_SECRET = "你的app_secret"

    api = JDCommentAPI(APP_KEY, APP_SECRET)
    # 获取商品评论(全部评论)
    res = api.get_goods_comments(goods_id="100012345678", page=1, page_size=20, comment_type=0)
    print(json.dumps(res, ensure_ascii=False, indent=2))




四、核心避坑要点(原创干货)


  1. 必须使用 13 位毫秒时间戳:10 位时间戳会直接签名失败

  2. 京东签名必须用 SHA256:不能使用 MD5,否则鉴权失败

  3. comment_type 可筛选评论类型:0 全部、1 好评、2 中评、3 差评、4 图 / 视频

  4. 请求必须加间隔:0.5~1 秒,避免触发 403 限流

  5. 商品必须支持评论获取:部分虚拟商品、无评价商品会返回空列表

群贤毕至

访客