> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.beatapi.io/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.beatapi.io/_mcp/server.

# Reddit 评论怎么按最新排序？怎样保存多层回复和翻页？

> 按真实 post_id、sort_type、commentForest 与 after 说明读取方法。

要先读 Reddit 帖子的最新评论，调用 `reddit.app.fetch_post_comments`，设 `sort_type:"NEW"`。帖子链接里的 `/comments/abc123/` 对应 `post_id:"t3_abc123"`。评论树还有下一页时，把返回的 `more.cursor` 作为下一次请求的 `after`。保存每个树条目的 `parentId`，回复关系就不会丢。

## 按最新、最高分还是推荐排序？

要最新评论就用 `NEW`。`sort_type` 接受 `CONFIDENCE`（默认）、`NEW`、`TOP`、`HOT`、`CONTROVERSIAL`、`OLD`、`RANDOM`，区分大小写。`TOP` 按评论分数排序，不会只返回一级评论。`need_format` 设为 false，得到本页所讲的原始树结构。

```bash
mkdir reddit-query-1 && \
curl --fail-with-body -sS https://api.beatapi.io/v1/social-data/call \
  -H "Authorization: Bearer $BEATAPI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"action":"reddit.app.fetch_post_comments","params":{"post_id":"t3_abc123","sort_type":"NEW","need_format":false,"after":""}}' \
  -D reddit-query-1/response.headers \
  -o reddit-query-1/reddit-comments.json
```

## 评论正文、分数和回复关系在哪？

先检查顶层 `status` 为 `succeeded`，再逐项读取 `data.postInfoById.commentForest.trees[]`：

| 数据              | 字段                                      |
| --------------- | --------------------------------------- |
| 评论正文            | `node.content.markdown`                 |
| 评论 ID           | `node.id`                               |
| 分数：赞踩差额，不等于点赞次数 | `node.score`                            |
| 来源链接            | `node.permalink`                        |
| 回复关系            | 树条目上的 `parentId`、`depth` 和 `childCount` |

`depth=0` 表示一级评论。`trees[]` 是全部返回节点的扁平列表，回复和一级评论都在 `trees[]` 里。`childCount` 的数值不代表响应里已有这么多条回复，回复条数按实际收到的节点统计。

## after 从哪里取？

读取 `data.postInfoById.commentForest.pageInfo.hasNextPage`：

* `false`：评论列表没有下一页。
* `true`：取 `trees[]` 最后一项的 `more.cursor`，作为下一次的 `after`；`post_id` 和 `sort_type` 保持不变。

游标按返回值原样传入。不能递增游标，也不能把搜索的 `endCursor` 或其他回复动作的 cursor 当作 `after`。

`hasNextPage` 为 true 但 `more.cursor` 为空时，停止并保存 `request_id`，不要重放第一页。后端登记的四个历史样本里 `more` 都是 null，所以代码必须处理游标为空的情况，不能猜字段。游标重复、业务失败或到达自己设定的页数上限也停止。达到页数上限只表示本批结束，帖子可能还有更多评论。

评论导出指南里的 CSV 导出脚本 `export_comments.py` 会保留已收到的全部有效评论，并报告一种停止原因：

* `missing_usable_cursor`：`hasNextPage` 为 true，但没有返回可用游标。
* `provider_end`：评论列表的分页已结束，可能仍有未展开的回复分支。
* `next_page_available`：有游标。请求下一页是一次单独收费的调用，由你决定是否发送。

导出脚本始终把 `all_comments_collected` 设为 false。同一响应内，同一评论 ID 出现两个不同节点时，导出停止。不同已存页面之间的变化保留在合并步骤的观察表里。

## 为什么拿到评论仍不等于取得全部回复？

不等于。评论树只包含本次响应返回的节点；隐藏或移除的内容、后续分页和未展开分支都不在树里。保留 `node.isRemoved`，不补写被删除的正文。按实际评论 ID 去重并统计。回复的父评论不在本页时，保留回复的 `parentId`，等合并其他页时再对上，不把回复改成一级评论。

[导出评论和回复关系](/zh/guides/reddit-comment-export) · [Reddit 问题索引](/zh/faq/reddit)

依据：`data:reddit.app.fetch_post_comments` 的输入合同、后端 2026-09-24 四个样本的输出结构，以及网关当前的渲染实现。

导出后接着走[人工审核问题清单](/zh/guides/reddit-comment-export)：一级评论和回复分别计数，每条支撑原句都对照原始响应核对。父评论缺失的回复仍按回复处理，不算作独立的一级投诉。评论数不等于遇到同类问题的客户比例。

## 从复制的链接生成正确帖子请求

取 `/comments/` 后紧跟的 ID 作为帖子 ID，不取路径最后一段：评论永久链接里先是帖子 ID，后面才是评论 ID。`t3_` 表示帖子，`t1_` 表示评论；把评论 ID 换成 `t3_` 前缀得不到帖子 ID。两种前缀的说明见 [Reddit 官方说明](https://developers.reddit.com/docs/capabilities/server/reddit-api)。

将下方脚本保存为 `reddit_post_request.py`。脚本接受小写 `t3_` 完整标识，或 reddit.com、[www.reddit.com、old.reddit.com、new.reddit.com](http://www.reddit.com、old.reddit.com、new.reddit.com) 上的标准 HTTPS 帖子或评论永久链接。裸 ID 看不出指的是帖子还是评论，脚本直接拒绝。`/s/` 分享链接、短链、社区首页和外站链接要先打开，复制最终的帖子链接；脚本不跟随跳转，也不产生付费查询。

```bash
python3 reddit_post_request.py 'https://www.reddit.com/r/example/comments/abc123/title/def456/' reddit-request
```

命令里的 ID 只是示例。对示例链接，`request.json` 指向 `t3_abc123`，`input-audit.json` 记下输入原本指向一条评论。请求读取所属帖子的评论第一页，不是只取评论 `def456`，也不会沿用分享链接在网页里的筛选条件。请求设置 NEW 排序、原始结构、首页空 after 和 en-US。en-US 是语言偏好，不会只筛出英语作者或英语正文。脚本拒绝写入已存在的输出目录。

```python
import json
import re
import sys
from pathlib import Path
from urllib.parse import urlsplit

value, output = sys.argv[1:]
post_id = value
comment_link = False
if not re.fullmatch(r"t3_[a-z0-9]{1,17}", value):
    url = urlsplit(value)
    if url.scheme != "https" or url.hostname not in {"reddit.com", "www.reddit.com", "old.reddit.com", "new.reddit.com"} or url.username or url.password or url.port not in (None, 443):
        raise SystemExit("Use an HTTPS Reddit post permalink or an explicit t3_ fullname")
    match = re.fullmatch(r"/(?:r/[A-Za-z0-9_]+/)?comments/([a-z0-9]{1,17})(?:/([^/]+)(?:/([a-z0-9]+))?)?/?", url.path)
    if not match:
        raise SystemExit("No supported post permalink; open shared/short links and copy the final post URL")
    post_id = "t3_" + match.group(1)
    comment_link = match.group(3) is not None
request = {"action": "reddit.app.fetch_post_comments", "params": {
    "post_id": post_id, "sort_type": "NEW", "need_format": False, "after": "", "language": "en-US"}}
folder = Path(output)
folder.mkdir(exist_ok=False)
(folder / "request.json").write_text(json.dumps(request, indent=2), encoding="utf-8")
(folder / "input-audit.json").write_text(json.dumps({
    "original_input": value, "post_id": post_id, "input_points_to_comment": comment_link,
    "scope": "first page of the containing post, not a selected comment or all replies",
    "post_exists_verified": False, "requests_executed": 0}, indent=2), encoding="utf-8")
print(json.dumps({"post_id": post_id, "requests_prepared": 1, "requests_executed": 0}))
```

发送生成的请求前，先用 Inspect 查 `reddit.app.fetch_post_comments` 的当前价格。本页编写时，Inspect 显示评论首页单价为 `$0.003`；生成 JSON 免费，后续每一页是一次独立调用。费用获批后，用自己的 Bearer key 把 `request.json` 发到 `/v1/social-data/call`，保存完整响应，再按 [CSV 导出与问题清单流程](/zh/guides/reddit-comment-export)处理。脚本只校验链接格式；帖子是否存在、是否公开、评论能否读取，以 API 响应为准。