评论线程是 Reddit 真正信号所在的地方。标题是营销文案,但回复决定了某个说法是否经得起审查。本教程演示如何通过单个 API 调用获取 Reddit 帖子及其完整线程评论树,然后使用平面、深度注释的评论数组来呈现、过滤或聚合。
前置条件
- Python 3.8 或更高版本
- 请求库
- Scavio API 密钥
- 您要分析的 Reddit 帖子 URL
操作指南
步骤 1: 选择 Reddit 帖子 URL
任何规范的 Reddit 帖子 URL 都可以。 /comments/<id>/ 路径就足够了。
Python
POST_URL = "https://www.reddit.com/r/Python/comments/1smb9du/fastapi_vs_django/"步骤 2: 获取帖子和评论
使用 URL POST 到 /api/v1/reddit/post。响应包括帖子以及平面评论数组。
Python
import os, requests
r = requests.post(
"https://api.scavio.dev/api/v1/reddit/post",
headers={"Authorization": f"Bearer {os.environ['SCAVIO_API_KEY']}"},
json={"url": POST_URL},
timeout=30,
)
data = r.json()["data"]步骤 3: 使用深度缩进渲染注释
每个评论都有一个从 0 开始的深度字段,表示顶级回复。将深度乘以两个空格即可获得可读的树。
Python
for c in data["comments"]:
indent = " " * c["depth"]
print(f"{indent}[{c['score']:>4}] u/{c['author']}: {c['body'][:100]}")步骤 4: 按深度聚合以进行快速分析
使用深度来总结顶级情绪与深层回复。
Python
from collections import defaultdict
by_depth = defaultdict(list)
for c in data["comments"]:
by_depth[c["depth"]].append(c["score"])
for depth, scores in sorted(by_depth.items()):
print(f"depth {depth}: {len(scores)} comments, avg score {sum(scores)/len(scores):.1f}")Python 示例
Python
import os, requests
API_KEY = os.environ["SCAVIO_API_KEY"]
def fetch_post(url: str):
r = requests.post(
"https://api.scavio.dev/api/v1/reddit/post",
headers={"Authorization": f"Bearer {API_KEY}"},
json={"url": url},
timeout=30,
)
r.raise_for_status()
return r.json()["data"]
data = fetch_post("https://www.reddit.com/r/Python/comments/1smb9du/")
print(data["post"]["title"])
for c in data["comments"][:20]:
print(" " * c["depth"] + f"u/{c['author']}: {c['body'][:80]}")JavaScript 示例
JavaScript
const API_KEY = process.env.SCAVIO_API_KEY;
async function fetchPost(url) {
const r = await fetch("https://api.scavio.dev/api/v1/reddit/post", {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ url }),
});
const { data } = await r.json();
return data;
}
const data = await fetchPost("https://www.reddit.com/r/Python/comments/1smb9du/");
console.log(data.post.title);
for (const c of data.comments.slice(0, 20)) {
console.log(" ".repeat(c.depth) + `u/${c.author}: ${c.body.slice(0, 80)}`);
}预期输出
JSON
FastAPI vs Django in 2026 -- what the teams are actually using
u/senior_py: We moved to FastAPI for the API surface and kept Django for admin
u/django_dev: Django ORM is still unmatched for anything with relational depth.
u/another_dev: Agreed -- the admin is a force multiplier for internal tools.