×

小红书笔记评论 API:Python 数据解析(超简单、通用、直接用)

知名用户18007905473 知名用户18007905473 发表于2026-04-10 16:07:46 浏览9 评论0

抢沙发发表评论

我给你最简单、最清晰、可直接复制的解析教程,不管返回什么结构,你都能轻松提取评论、用户、点赞、时间、回复。

一、先看标准返回结构(你要解析的就是这个)

json

{

  "code": 200,
  "msg": "success",
  "data": {
    "note_id": "67a12345",
    "total": 128,
    "cursor": "next_cursor_123",
    "has_more": true,
    "comments": [
      {
        "comment_id": "c_123",
        "content": "这个太好用了!",
        "create_time": 1744000000,
        "like_count": 25,
        "user": {
          "user_id": "u_456",
          "nickname": "草莓酱",
          "avatar": "https://xxx.jpg"
        },
        "reply_comments": [
          {
            "comment_id": "r_789",
            "content": "求链接~",
            "create_time": 1744000010,
            "user": { "nickname": "小明" }
          }
        ]
      }
    ]
  }}

二、Python 万能解析代码(直接复制运行)

python

运行

def parse_xhs_comments(json_data):

    """
    解析小红书评论API返回的JSON
    返回:干净的评论列表(可直接入库/导出)
    """
    result = []

    # 1. 先判断请求是否成功
    if json_data.get("code") != 200:
        print("请求失败:", json_data.get("msg"))
        return result    # 2. 进入数据层
    data = json_data.get("data", {})
    comments = data.get("comments", [])

    # 3. 循环解析每条评论
    for c in comments:
        # 基础信息
        comment_info = {
            "评论ID": c.get("comment_id"),
            "内容": c.get("content"),
            "点赞数": c.get("like_count"),
            "时间": c.get("create_time"),  # 时间戳
            "用户ID": c.get("user", {}).get("user_id"),
            "用户名": c.get("user", {}).get("nickname"),
            "用户头像": c.get("user", {}).get("avatar"),
        }

        # 4. 解析楼中楼回复(可选)
        reply_list = []
        for reply in c.get("reply_comments", []):
            reply_list.append({
                "回复ID": reply.get("comment_id"),
                "回复内容": reply.get("content"),
                "回复人": reply.get("user", {}).get("nickname")
            })
        
        comment_info["回复列表"] = reply_list
        result.append(comment_info)

    return result

三、如何使用?

python

运行

# 假设这是你调用API得到的json数据json_result = requests.get(...).json()# 解析comments_list = parse_xhs_comments(json_result)# 打印看看for item in comments_list:

    print("用户名:", item["用户名"])
    print("评论内容:", item["内容"])
    print("点赞:", item["点赞数"])
    print("回复:", item["回复列表"])
    print("-" * 60)

四、你能解析出哪些数据?(全部都能拿)

  • 评论 ID

  • 评论文字内容

  • 点赞数

  • 发布时间(时间戳)

  • 用户 ID

  • 用户名

  • 用户头像

  • 楼中楼回复(多条)

  • 回复内容、回复人

  • 评论图片 / 视频(如有)


五、最常用的 3 个解析技巧(新手必看)

1)安全取值(防止报错)

不要直接写:
python

运行

c["user"]["nickname"]  # 可能报错

要写:
python

运行

c.get("user", {}).get("nickname", "匿名用户")

2)时间戳转正常时间

python

运行

import time

create_time = 1744000000normal_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(create_time))print(normal_time)

3)只提取纯文本评论

python

运行

contents = [c["content"] for c in comments_list]


六、一句话总结

小红书评论解析 =
  1. 判断 code == 200

  2. data → comments

  3. 循环提取 content、user、like_count、reply_comments

  4. 转成干净列表


群贤毕至

访客