Skip to main content

Analyzing YouTube Comment Sentiment with Python

Β· 4 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Analyzing the "vibe" of a YouTube comment section is a fantastic way to use Python for data science. Since yt-dlp doesn't always handle large-scale comment scraping easily, we'll use the YouTube Data API v3 (the official way) and the TextBlob library to perform the sentiment analysis.

This script will tell you if the first 100 commenters are mostly happy, angry, or neutral.


🎭 The Sentiment Analysis Pipeline​

To do this, we need to:

  1. Get a YouTube API Key (Free from Google Cloud Console).
  2. Fetch Comments using the commentThreads endpoint.
  3. Calculate Polarity: A score from $-1$ (very negative) to $1$ (very positive).

πŸ’» The Implementation​

I've combined the API fetching and the sentiment logic into one block for you.

# 🧠 Python Script: YouTube Comment Sentiment Analyzer

### 1. Requirements
```bash
pip install google-api-python-client textblob

2. The Code​

from googleapiclient.discovery import build
from textblob import TextBlob
import re

# --- CONFIGURATION ---
API_KEY = "YOUR_YOUTUBE_API_KEY_HERE"
VIDEO_URL = "[https://www.youtube.com/watch?v=dQw4w9WgXcQ](https://www.youtube.com/watch?v=dQw4w9WgXcQ)"

def get_video_id(url):
pattern = r'(?:v=|\/)([0-9A-Za-z_-]{11}).*'
match = re.search(pattern, url)
return match.group(1) if match else None

def analyze_sentiment(video_url):
video_id = get_video_id(video_url)
youtube = build('youtube', 'v3', developerKey=API_KEY)

# 1. Fetch the first 100 comments
request = youtube.commentThreads().list(
part="snippet",
videoId=video_id,
maxResults=100,
textFormat="plainText"
)
response = request.execute()

polarities = []

print(f"πŸ“Š Analyzing comments for: {video_id}...\n")

for item in response['items']:
comment = item['snippet']['topLevelComment']['snippet']['textDisplay']

# 2. Perform Sentiment Analysis
analysis = TextBlob(comment)
# polarity: -1 (negative) to 1 (positive)
polarities.append(analysis.sentiment.polarity)

# 3. Calculate Results
if not polarities:
return "No comments found."

avg_sentiment = sum(polarities) / len(polarities)

# 4. Interpret the Score
if avg_sentiment > 0.1:
mood = "🌟 POSITIVE"
elif avg_sentiment < -0.1:
mood = "πŸ’’ NEGATIVE"
else:
mood = "😐 NEUTRAL"

print(f"Average Sentiment Score: {avg_sentiment:.2f}")
print(f"Overall Audience Mood: {mood}")

# --- Run ---
analyze_sentiment(VIDEO_URL)


πŸ“ˆ Understanding the Polarity Score​

TextBlob uses a pre-trained model to assign a value to words.

Score RangeSentimentCommon Words Found
0.5 to 1.0Very Positive"Amazing", "Masterpiece", "Love", "Helpful"
-0.1 to 0.1Neutral"Okay", "Video", "Question", "Information"
-1.0 to -0.5Very Negative"Hate", "Terrible", "Waste", "Broken"

πŸ“š Sources & Technical Refs​


πŸ“‹ Pro Tip: Subjectivity vs. Polarity​

The analysis.sentiment object also returns Subjectivity (0 to 1).

  • 0.0 is very objective (fact-based).
  • 1.0 is very subjective (opinion-based). Checking this helps you filter out "spam" or "bot" comments that are just repeating facts versus real people sharing feelings.

More on python