小红书笔记的评论正文能按原帖导出 CSV 吗?
可以用一篇公开笔记的 ID 或链接查询评论正文,将每条评论保存为一行。小红书关键词工作台中的评论字段是计数;正文使用扩展评论接口。
DataXAPI 价目生效日期与版本 2026-11-25 · v22
对单篇笔记读取评论
实时扩展接口使用带 catalog 权限的 Social2api API Key,并通过 X-DataXAPI-Key 提供自己的 DataXAPI Key。
GET /v1/xiaohongshu/notes/comments/list 传 id 或 url,可选 sort=latest 或 sort=popular。同方向翻页时保持原始标识和排序,把 page.next_cursor 作为下一次 cursor。目录单页上限为 20。
导出时关联笔记并检查缺失
把 data.items 展开为 comment_id、post_id、text、published_at、author.name、metrics.likes。对话题下多篇笔记逐篇查评论、记录本次页数;没有正文的条目保留空值。CSV 是调用方从 JSON 生成的。
用关键词跑多作品抽样并导出两级评论
先搜索两页、取最多 3 个有效作品,分别读取两页一级评论。对每个作品只选一个父评论展开两页回复。本例最大请求量 2 + 6 + 6 = 14。DataXAPI 通道都计费时,搜索每页 USD 0.02,评论和回复每页 USD 0.02,合计 USD 0.28。回复只代表所选父评论。
保存为 comments.py,在设好关键词与两把 Key 后运行。CSV 保留输入归属、原始 parent_id/root_id、请求父评论 ID 和正文。receipt.json 记录游标、警告、错误和上游请求号;按本次预算和公开可见性说明覆盖。
import csv, json, os
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import Request, urlopen
base = 'https://social2api.com'
headers = {'Authorization': 'Bearer ' + os.environ['SOCIAL2API_API_KEY'],
'X-DataXAPI-Key': os.environ['DATAXAPI_API_KEY']}
term = os.environ['SOCIAL2API_QUERY']
receipts = []
def pages(path, params, cap, limit=2):
cursor = None
for _ in range(limit):
query = {**params, **({'cursor': cursor} if cursor else {})}
request = Request(base + path + '?' + urlencode(query),
headers={**headers, 'X-Max-Price': cap})
try:
with urlopen(request, timeout=90) as response:
body = json.load(response)
except HTTPError as error:
receipts.append({'path': path, 'http': error.code, 'params': query})
return
paging = body.get('page') or {}
receipts.append({'path': path, 'params': query, 'page': paging,
'request_id': body.get('request_id'), 'warnings': body.get('warnings', [])})
yield from body['data']['items']
cursor = paging.get('next_cursor')
if not paging.get('has_more') or not cursor:
return
def export(name, rows):
fields = ['comment_id', 'post_id', 'parent_id', 'root_id', 'requested_parent_id', 'text', 'published_at']
with open(name, 'w', encoding='utf-8-sig', newline='') as file:
writer = csv.DictWriter(file, fields)
writer.writeheader()
writer.writerows(rows.values())
def record(item, post_id, requested_parent_id=None):
return {'comment_id': item.get('id'), 'post_id': post_id, 'parent_id': item.get('parent_id'),
'root_id': item.get('root_id'), 'requested_parent_id': requested_parent_id,
'text': item.get('text'), 'published_at': item.get('published_at')}
post_ids = list(dict.fromkeys(item['id'] for item in pages('/v1/xiaohongshu/notes/search', {'query': term}, '0.02') if item.get('id')))[:3]
top, children = {}, {}
for post_id in post_ids:
selected_parent = None
for item in pages('/v1/xiaohongshu/notes/comments/list', {'id': post_id}, '0.02'):
if item.get('id'):
top[post_id, item['id']] = record(item, post_id)
selected_parent = selected_parent or item['id']
if selected_parent:
for item in pages('/v1/xiaohongshu/comments/replies/list', {'id': selected_parent, 'post_id': post_id}, '0.02'):
if item.get('id'):
children[post_id, item['id']] = record(item, post_id, selected_parent)
export('comments.csv', top)
export('replies.csv', children)
with open('receipt.json', 'w', encoding='utf-8') as file:
json.dump(receipts, file, ensure_ascii=False, indent=2)