Les fils de commentaires sont là où se trouve le vrai signal de Reddit. Le titre est un texte marketing, mais les réponses déterminent si une affirmation résiste à l'examen. Ce tutoriel montre comment récupérer un post Reddit et son arbre de commentaires complet avec un seul appel API, puis travailler avec le tableau plat de commentaires annotés par profondeur pour les afficher, filtrer ou agréger.
Prérequis
- Python 3.8 ou supérieur
- bibliothèque requests
- Une clé API Scavio
- Une URL de post Reddit que vous souhaitez analyser
Parcours
Étape 1: Choisissez une URL de post Reddit
Toute URL canonique de post Reddit fonctionne. Le chemin /comments/<id>/ suffit.
POST_URL = "https://www.reddit.com/r/Python/comments/1smb9du/fastapi_vs_django/"Étape 2: Récupérer le post et les commentaires
POST sur /api/v1/reddit/post avec l'URL. La réponse inclut le post ainsi que le tableau plat des commentaires.
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"]Étape 3: Afficher les commentaires avec une indentation de profondeur
Chaque commentaire a un champ depth commençant à 0 pour les réponses de premier niveau. Multipliez depth par deux espaces pour un arbre lisible.
for c in data["comments"]:
indent = " " * c["depth"]
print(f"{indent}[{c['score']:>4}] u/{c['author']}: {c['body'][:100]}")Étape 4: Agréger par profondeur pour une analyse rapide
Utilisez depth pour résumer le sentiment de premier niveau séparément des réponses profondes.
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}")Exemple 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]}")Exemple 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)}`);
}Sortie attendue
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.