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