Reddit 主题中的情绪分析让您一目了然地了解社区是热情、怀疑还是分裂。通过一次 API 调用,您可以提取帖子和所有评论,并且通过简短的 LLM 提示,您可以对每个回复进行分类和聚合。本教程使用 Scavio 进行获取,使用 Anthropic 的 Claude 进行分类,但该结构适用于任何提供者。
前置条件
- Python 3.10+
- 人择和请求库
- Scavio API 密钥和 Anthropic API 密钥
- 要分析的 Reddit 话题 URL
操作指南
步骤 1: 获取线程
一次调用即可拉取该帖子以及所有评论。
Python
import os, requests
resp = requests.post(
"https://api.scavio.dev/api/v1/reddit/post",
headers={"Authorization": f"Bearer {os.environ['SCAVIO_API_KEY']}"},
json={"url": "https://www.reddit.com/r/Python/comments/1smb9du/"},
timeout=30,
)
comments = resp.json()["data"]["comments"]步骤 2: 对每个评论进行分类
将每个评论正文发送给克劳德并要求一个标签:正面、中立或负面。
Python
from anthropic import Anthropic
client = Anthropic()
def classify(text: str) -> str:
msg = client.messages.create(
model="claude-opus-4-6",
max_tokens=10,
messages=[{"role": "user", "content": f"One word: positive, neutral, or negative?\n\n{text}"}],
)
return msg.content[0].text.strip().lower()步骤 3: 汇总并报告
对顶级评论运行分类、计算标签并打印摘要。
Python
from collections import Counter
top_level = [c for c in comments if c["depth"] == 0][:25]
labels = [classify(c["body"]) for c in top_level]
print(Counter(labels))Python 示例
Python
import os, requests
from collections import Counter
from anthropic import Anthropic
SCAVIO_KEY = os.environ["SCAVIO_API_KEY"]
client = Anthropic()
def fetch_thread(url: str):
r = requests.post(
"https://api.scavio.dev/api/v1/reddit/post",
headers={"Authorization": f"Bearer {SCAVIO_KEY}"},
json={"url": url},
timeout=30,
)
r.raise_for_status()
return r.json()["data"]
def classify(text: str) -> str:
msg = client.messages.create(
model="claude-opus-4-6",
max_tokens=10,
messages=[{"role": "user", "content": f"One word: positive, neutral, or negative?\n\n{text}"}],
)
return msg.content[0].text.strip().lower()
data = fetch_thread("https://www.reddit.com/r/Python/comments/1smb9du/")
top = [c for c in data["comments"] if c["depth"] == 0][:25]
labels = [classify(c["body"]) for c in top]
print("Sentiment on", data["post"]["title"])
print(Counter(labels))JavaScript 示例
JavaScript
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const SCAVIO_KEY = process.env.SCAVIO_API_KEY;
async function fetchThread(url) {
const r = await fetch("https://api.scavio.dev/api/v1/reddit/post", {
method: "POST",
headers: {
Authorization: `Bearer ${SCAVIO_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ url }),
});
return (await r.json()).data;
}
async function classify(text) {
const msg = await client.messages.create({
model: "claude-opus-4-6",
max_tokens: 10,
messages: [{ role: "user", content: `One word: positive, neutral, or negative?\n\n${text}` }],
});
return msg.content[0].text.trim().toLowerCase();
}
const data = await fetchThread("https://www.reddit.com/r/Python/comments/1smb9du/");
const top = data.comments.filter((c) => c.depth === 0).slice(0, 25);
const labels = await Promise.all(top.map((c) => classify(c.body)));
const counts = labels.reduce((a, l) => ({ ...a, [l]: (a[l] ?? 0) + 1 }), {});
console.log(counts);预期输出
JSON
Sentiment on FastAPI vs Django in 2026 -- what the teams are actually using
Counter({'positive': 14, 'neutral': 8, 'negative': 3})