Analyzing YouTube Comment Sentiment with Python
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:
- Get a YouTube API Key (Free from Google Cloud Console).
- Fetch Comments using the
commentThreadsendpoint. - 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 Range | Sentiment | Common Words Found |
|---|---|---|
| 0.5 to 1.0 | Very Positive | "Amazing", "Masterpiece", "Love", "Helpful" |
| -0.1 to 0.1 | Neutral | "Okay", "Video", "Question", "Information" |
| -1.0 to -0.5 | Very Negative | "Hate", "Terrible", "Waste", "Broken" |
π Sources & Technical Refsβ
- [1.1] Google Cloud: YouTube Data API Overview - How to get your free API key.
- [2.1] TextBlob Docs: Sentiment Analysis API - Understanding how polarity and subjectivity are calculated.
- [3.1] NLTK: Natural Language Toolkit - The underlying engine many Python sentiment tools use.
π 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.
