← Back to list

How to Build a YouTube Comment Dataset for Sentiment Analysis, NLP, Other Analysis and Conduct…

You already know Youtube is one of the world’s largest platforms and media for public discussions, containing billions of users comments…

Bakkaprabhu Uppar · 2026-06-04 14:54 · 2 claps · 3.2 min read
#research #python #youtube-api #data-collection #sentiment-analysis
Open on Medium ↗
Wiki topics: 🎙️ · Creator Economy

How to Build a YouTube Comment Dataset for Sentiment Analysis, NLP, Other Analysis and Conduct Research Using Python

You already know Youtube is one of the world’s largest platforms and media for public discussions, containing billions of users comments across topics such as politics, education, and what not.

For research scholars like you and me, youtube comments provide a valuable source of textual data that can be used for:

  • Sentiment analysis
  • Public opinion studies
  • Misinformation research
  • Political communication analysis
  • Consumer behavior research
  • Educational content evaluation
  • Social network and engagement studies
  • Natural Language Processing (NLP) projects

Rather than manually collecting comments, researchers can use the YouTube Data API to systematically retrieve comment datasets for analysis.

The goal of this post is to collect publicly available comments from a YouTube video and export them into a structured dataset suitable for analysis.

Will use, Python, YouTube Data API v3, Pandas

Step 1: Head over to research colab and paste the below code

As shown in image

# Install required library
!pip install pandas google-api-python-client

from googleapiclient.discovery import build
import pandas as pd

# 🔑 Replace with your API key
API_KEY = "REPLACE WITH YOUR KEY"

# 🎯 Your video ID (from your link)
VIDEO_ID = "VIDEO ID"

# Build API
youtube = build('youtube', 'v3', developerKey=API_KEY)

# Function to get comments
def get_comments(video_id):
    comments = []

    request = youtube.commentThreads().list(
        part="snippet",
        videoId=video_id,
        maxResults=100
    )

    while request:
        response = request.execute()

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

            comments.append({
                'author': comment['authorDisplayName'],
                'comment': comment['textDisplay'],
                'likes': comment['likeCount'],
                'published_at': comment['publishedAt']
            })

        request = youtube.commentThreads().list_next(request, response)

    return comments

# Fetch data
data = get_comments(VIDEO_ID)

# Convert to DataFrame
df = pd.DataFrame(data)

# Save to Excel
df.to_excel("youtube_comments.xlsx", index=False)

print("✅ File saved as youtube_comments.xlsx")

Step 2: Run the code using ctrl+enter, and after few minutes you’ll see the excel file in files section, right click and download it

Required information: you need youtube api key, and video id

For example if a video link is https://youtu.be/s7aOJYueEtI?si=fHTYOahCGKRxJpgs

The video id will be: (after slash and before question mark)

s7aOJYueEtI

For youtube API follow these steps:

How to Get a YouTube Data API Key (Step-by-Step)

Before running the script, you’ll need a YouTube Data API key from Google Cloud.

Step 1: Open Google Cloud Console

Visit the Google Cloud Console and sign in with your Google account.

https://console.cloud.google.com/

Step 2: Create a New Project

  1. Click the Project Selector at the top of the page.
  2. Click New Project.
  3. Enter a project name such as:
YouTube Research Project
  1. Click Create.

Wait a few seconds for the project to be created.

Step 3: Select Your Project

After creation:

  1. Click the project selector again.
  2. Choose the newly created project.

All resources and API usage will now be associated with this project.

Step 4: Enable the YouTube Data API v3

  1. In the left menu, navigate to:
APIs & Services → Library
  1. Search for:
YouTube Data API v3
  1. Click on the API.
  2. Click Enable.

This allows your project to access YouTube data programmatically.

Step 5: Create API Credentials

  1. Navigate to:
APIs & Services → Credentials
  1. Click:
+ CREATE CREDENTIALS
  1. Select:
API Key

Google will generate a new API key instantly.

It will look similar to:

AIzaSyXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Step 6: Copy and Store Your API Key

Copy the generated key and store it securely.

In your Python script:

API_KEY = "YOUR_API_KEY"

Replace YOUR_API_KEY with the key you just generated.

Step 7 (Recommended): Restrict the API Key

For security purposes:

  1. Click on the newly created API key.
  2. Under API Restrictions, select:
Restrict Key

Choose:

YouTube Data API v3

Click Save.

This prevents the key from being used with other Google APIs.

Step 8: Test Your Setup

Run your Python script.

If everything is configured correctly, the script should:

  • Connect to the YouTube Data API
  • Retrieve comments from the specified video
  • Export the data to an Excel file

Example output:

Dataset exported successfully.

Common Errors and Fixes

Error: Access Not Configured

Cause: The YouTube Data API has not been enabled.

Solution: Return to the API Library and enable YouTube Data API v3.

Error: API Key Not Valid

Cause: The API key was copied incorrectly.

Solution: Generate a new key and update your script.

Error: Quota Exceeded

Cause: Daily API quota limit has been reached.

Solution: Wait for the quota reset or request additional quota through Google Cloud.

Security Reminder

Never publish your API key on GitHub, Medium articles, research papers, or public repositories.

Always replace it with:

API_KEY = "YOUR_API_KEY"

before sharing your code.


메타데이터
post_id
616b8ba81208
slug
how-to-build-a-youtube-comment-dataset-for-sentiment-analysis-nlp-other-analysis-and-conduct-616b8ba81208
url
https://medium.com/@techcrazebk/how-to-build-a-youtube-comment-dataset-for-sentiment-analysis-nlp-other-analysis-and-conduct-616b8ba81208
canonical_url
https://medium.com/@techcrazebk/how-to-build-a-youtube-comment-dataset-for-sentiment-analysis-nlp-other-analysis-and-conduct-616b8ba81208
author_url
https://medium.com/@techcrazebk
status
ok
fetched_at
2026-06-09 15:37:30