This browser does not support JavaScript

Scrape YouTube Comments in 2025: Step-by-Step Guide

教程
OkeyProxy

As of 2025, YouTube has approximately 2.7 billion monthly active users worldwide, making it the second-largest social media platform after Facebook. 

Additionally, YouTube has over 122 million daily active users. India leads with the highest number of users at around 491 million, followed by the United States with approximately 253 million, making it one of the best platforms to get raw data especially the comments section where it offers raw, unfiltered insights into audience sentiments, trends, and preferences. 

For data analysts, developers, or businesses, scraping YouTube comments can unlock valuable data for sentiment analysis, market research, or content strategy optimization.

This tutorial walks you through building a YouTube comment scraper using Python, covering setup, code, proxy integration, and an API-based alternative. 

Scrape YouTube data for insights

Why Scrape YouTube Comments?

YouTube comments are a treasure trove of real-time user feedback. Businesses can use this data to:

Consumer Sentiment Analysis

Comments often reflect what viewers really think, whether it's excitement, confusion, praise, or criticism. Scraping comments helps businesses:

 ● Detect positive/negative sentiment toward products or campaigns.

 ● Perform text analysis to understand emotional drivers.

 ● Monitor public reception of influencer or brand content.

Market & Trend Research

You can extract comments over time to spot:

 ● Trending topics (e.g., viral phrases, memes, recurring mentions).

 ● Emerging needs or pain points that your product could address.

 ● Reactions to competitor content for competitive intelligence.

Influencer & Brand Monitoring

Scraping comment sections on sponsored content helps:

 ● Measure engagement quality, not just quantity (e.g., meaningful responses vs spam).

 ● Analyze brand mentions and how users perceive collaborations.

 ● Identify ideal influencers based on audience feedback.

Content & UX Feedback

For creators and product teams:

 ● Understand viewer confusion or areas where your content failed to communicate.

 ● Discover feature requests and real-world usage patterns.

 ● Gather community feedback at scale for content iteration.

Whether you’re a data analyst building sentiment models or a marketer refining campaigns, scraping YouTube comments provides actionable insights.

Setting Up Your Environment

To scrape YouTube comments, you’ll need a Python environment with the right libraries. Here’s how to set it up:

Prerequisites

 ● Python 3.8+: Ensure Python is installed (download from python.org if needed).

 ● pip: Python’s package manager for installing libraries.

 ● Code Editor: Use VS Code, PyCharm, or any editor you prefer.

 ● Browser: Chrome or Firefox for inspecting YouTube’s HTML structure.

Required Libraries

Install these Python libraries using pip:

pip install requests beautifulsoup4 pandas

 ● requests: Fetches YouTube page content via HTTP requests.

 ● BeautifulSoup4: Parses HTML to extract comments and metadata.

 ● pandas: Structures scraped data and exports it to CSV.

Tip: Run pip install -U requests beautifulsoup4 pandas to ensure you have the latest versions.

Fetching a YouTube Video Page

Let’s start with a simple script to fetch a YouTube video page. YouTube’s dynamic content can make scraping tricky, so we’ll use headers to mimic a browser and avoid blocks.

python

import requests

from bs4 import BeautifulSoup

 

# Target YouTube video URL

url = "https://www.youtube.com/watch?v=VIDEO_ID"

 

# Headers to mimic a browser

headers = {

 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"

}

 

# Fetch the page

response = requests.get(url, headers=headers)

 

# Check for successful response

if response.status_code == 200:

 soup = BeautifulSoup(response.text, "html.parser")

 print("Page fetched successfully!")

else:

 print(f"Failed to fetch page. Status code: {response.status_code}")

Common Issues and Fixes

 ● 403 Forbidden Error: YouTube may block requests without a proper User-Agent. Use the headers above to mimic a browser.

 ● CAPTCHAs: If YouTube detects bot-like behavior, it may serve a CAPTCHA. We’ll address this with proxies later.

 ● Dynamic Content: YouTube loads comments via JavaScript. For now, we’ll scrape static HTML comments, but an API approach (covered later) handles dynamic content better.

Extracting Comments Step-by-Step

To scrape comments, we need to locate them in YouTube’s HTML using browser inspection tools.

 1.  Inspect the Page

Open the target YouTube video in Chrome/Firefox.

 a.  Right-click a comment and select “Inspect” to open DevTools.

 b.  Identify the HTML element containing comments (usually <ytd-comment-thread-renderer> or <div id="contents">).

 2.  Parse Comments with BeautifulSoup

Here’s how to extract comment text and author names.

python

import requests

from bs4 import BeautifulSoup

 

url = "https://www.youtube.com/watch?v=VIDEO_ID"

headers = {

 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"

}

 

response = requests.get(url, headers=headers)

soup = BeautifulSoup(response.text, "html.parser")

 

# Find comment elements

comments = soup.find_all("ytd-comment-renderer")

 

data = []

for comment in comments:

 # Extract comment text

 text = comment.find("yt-formatted-string", {"id": "content-text"})

 text = text.get_text(strip=True) if text else "N/A"

 

 # Extract author

 author = comment.find("a", {"id": "author-text"})

 author = author.get_text(strip=True) if author else "N/A"

 

 data.append({"author": author, "comment": text})

 

print(data)

Note: YouTube’s HTML structure may change. Always verify element classes using DevTools.

Exporting Data to CSV

Use pandas to save the scraped comments in a structured CSV file.

python

import pandas as pd

 

# Assuming 'data' is the list from the previous script

df = pd.DataFrame(data)

df.to_csv("youtube_comments.csv", index=False)

print("Comments saved to youtube_comments.csv")

This creates a youtube_comments.csv file with columns for author and comment text. 

Looping Over Multiple Videos

To scrape comments from multiple videos, create a list of URLs and iterate through them.

python

import requests

from bs4 import BeautifulSoup

import pandas as pd

 

urls = [

 "https://www.youtube.com/watch?v=VIDEO_ID_1",

 "https://www.youtube.com/watch?v=VIDEO_ID_2"

]

 

headers = {

 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"

}

 

all_data = []

for url in urls:

 response = requests.get(url, headers=headers)

 if response.status_code == 200:

 soup = BeautifulSoup(response.text, "html.parser")

 comments = soup.find_all("ytd-comment-renderer")

 

 for comment in comments:

 text = comment.find("yt-formatted-string", {"id": "content-text"})

 text = text.get_text(strip=True) if text else "N/A"

 author = comment.find("a", {"id": "author-text"})

 author = author.get_text(strip=True) if author else "N/A"

 all_data.append({"video_url": url, "author": author, "comment": text})

 

# Export to CSV

df = pd.DataFrame(all_data)

df.to_csv("multiple_videos_comments.csv", index=False)

print("Comments from multiple videos saved to multiple_videos_comments.csv")

Integrating Proxies to Avoid Blocks

YouTube may block your IP if you send too many requests. Rotating proxies help avoid this. OkeyProxy provides reliable, rotating IPs for seamless scraping.

Using OkeyProxy

 1.  Sign up for OkeyProxy and get your proxy credentials.

 2.  Configure the proxy in your script.

python

import requests

from bs4 import BeautifulSoup

 

url = "https://www.youtube.com/watch?v=VIDEO_ID"

headers = {

 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"

}

 

# OkeyProxy configuration

proxies = {

 "http": "http://username:[email protected]:port",

 "https": "http://username:[email protected]:port"

}

 

response = requests.get(url, headers=headers, proxies=proxies)

if response.status_code == 200:

 soup = BeautifulSoup(response.text, "html.parser")

 print("Page fetched successfully with proxy!")

else:

 print(f"Failed to fetch page. Status code: {response.status_code}")

Tip: OkeyProxy’s rotating proxies automatically switch IPs, reducing the risk of bans.

Using a Scraper API for Simplicity

For a more scalable solution, use a scraper API to handle dynamic content and CAPTCHAs. Here’s an example using a POST request to a hypothetical scraper API.

python

import requests

import json

 

url = "https://api.scraperapi.com"

payload = {

 "api_key": "YOUR_API_KEY",

 "url": "https://www.youtube.com/watch?v=VIDEO_ID"

}

 

response = requests.post(url, json=payload)

if response.status_code == 200:

 data = response.json()

 print("Scraped data:", data)

else:

 print(f"API request failed. Status code: {response.status_code}")

This approach offloads rendering and proxy management to the API, ideal for large-scale scraping.

Comparing Scraping Approaches

Approach Pros Cons Ideal Use Case
Manual Scraping Free, full control, customizable Prone to blocks, handles static content only Small-scale, one-off projects
Proxy-Enabled Avoids IP bans, scalable Requires proxy setup, moderate cost Medium-scale, frequent scraping
Scraper API Handles dynamic content, no blocks, easy Higher cost, less control Large-scale, production-grade scraping

What Is OkeyProxy?

OkeyProxy is a leading proxy provider offering rotating residential IPs to bypass IP bans and CAPTCHAs. With global coverage and easy integration, it’s perfect for scaling web scraping projects. Try OkeyProxy’s free trial to get started.

FAQs

1.  Why do I keep getting 403 errors when scraping YouTube?

YouTube likely detects your requests as bot-like. Use proper headers or OkeyProxy’s rotating proxies to mimic human traffic.

2.  How do I configure OkeyProxy for multiple URLs?

Set up rotating proxies in your script using OkeyProxy’s credentials. Their dashboard provides easy configuration options.

3.  What if YouTube’s HTML structure changes?

Regularly inspect the page with DevTools to update your selectors. Alternatively, use a scraper API to handle dynamic content.

4.  Can I scrape comments for sentiment analysis?

Yes! Export comments to CSV and use NLP tools like TextBlob or Hugging Face transformers for sentiment analysis.

5.  How do I troubleshoot proxy connection issues?

Verify your proxy credentials and ensure the proxy server is active. 

Conclusion

Scraping YouTube comments offers valuable insights for businesses and analysts, but it requires careful handling to avoid blocks and ensure ethical use. Use OkeyProxy for reliable proxy integration or a scraper API for simplicity. Always respect YouTube’s terms of service and local data regulations. 

For more tools and tips, check out OkeyProxy’s free trial or explore related articles on web scraping best practices.