Scrape TikTok Comments for Sentiment Analysis (Step-by-Step)

Views tell you how far a video travelled. Comments tell you how people felt about it. For brand monitoring, product feedback or campaign reports, the comment section is the most honest data TikTok has.

This tutorial exports comments from a TikTok video with the TikTok Comment API and scores them with a simple sentiment model.

What the comment data looks like

Each item in the dataset is one comment:

Step 1: Run the Actor

The input has only four fields:

{
  "url": "https://www.tiktok.com/@ladygaga/video/7211250685902359850",
  "limit": 500,
  "shouldScrapeReplies": true,
  "region": "US"
}

Paste it into the Actor’s input on Apify and press Start, or call it from Python:

import os
from apify_client import ApifyClient

client = ApifyClient(os.environ["APIFY_TOKEN"])
run = client.actor("novi/tiktok-comment-api").call(run_input={
    "url": "https://www.tiktok.com/@ladygaga/video/7211250685902359850",
    "limit": 500,
    "shouldScrapeReplies": True,
})
comments = list(client.dataset(run["defaultDatasetId"]).iterate_items())
print(len(comments), "comments")

Step 2: Score sentiment

VADER is a small, rule-based model that handles emojis and slang well, which suits TikTok.

pip install vaderSentiment pandas
import pandas as pd
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

analyzer = SentimentIntensityAnalyzer()

df = pd.DataFrame([
    {
        "text": c.get("text", ""),
        "likes": c.get("digg_count", 0),
        "language": c.get("comment_language"),
    }
    for c in comments
])
df = df[df.language == "en"]
df["score"] = df.text.apply(lambda t: analyzer.polarity_scores(t)["compound"])
df["label"] = pd.cut(df.score, [-1, -0.05, 0.05, 1], labels=["negative", "neutral", "positive"], include_lowest=True)

print(df.label.value_counts(normalize=True).round(3))

Step 3: Weight by likes

A comment with 5,000 likes speaks for more people than one with zero. Weighting by likes gives a better picture of crowd opinion:

weighted = df.groupby("label", observed=True).likes.sum() / df.likes.sum()
print(weighted.round(3))

print(df.sort_values("likes", ascending=False).head(10)[["text", "likes", "label"]])

The top-liked comments are often the best quotes for a report.

Going further

Try TikTok Comment API

Super Fast TikTok Comment API. Seamlessly retrieve comments with speed and efficiency. You don't need to pay for proxy because we use our own.