All articles

Scraping YouTube Comments using Python & OpenAI for Sentiment Analysis

Published Date Jul 10, 2025
Read 7 min
Scraping YouTube Comments using Python & OpenAI for Sentiment Analysis

TL;DR

  • Scrape comments via Scrapingdog: GET https://api.scrapingdog.com/youtube/comments/?api_key=&v=VIDEO_ID; paginate with next_page_token.

  • Python loops first 3 pages to build all_comments.

  • Send comments to openrouter (openai/gpt-4.1-nano) to return a 2-line sentiment summary.

  • Demo outcome: sentiment is mostly negative toward Elon’s new party.

YouTube is more than just a video platform; it’s a goldmine of audience opinions, feedback, and emotions. Every comment under a video reflects how people feel, react, or engage with content, making it a valuable resource for brands, researchers, and creators.

But manually reading thousands of comments? Not practical.

In this tutorial, we’ll learn how we can scrape YouTube comments and how we can do sentiment analysis on those comments. We’ll be using Python to tie it all together. By the end, you’ll have a working script that scrapes YouTube comments and instantly tells you what the audience really thinks.

Prerequisites

  • Scrapingdog API key. You can get it by signing up here.

  • OpenAI API key.

  • Python 3.x. You can download it from here.

  • requestsand openai installed
    You can install the libraries using:

    `pip install requests openai`

Data flow

Scrape YouTube Comments

For this step, we will use Scrapingdog’s YouTube scraping API. This API will help us pull comments from any video. It is advisable to read the documentation first before moving ahead with this blog.

I have also created a video to guide you how to use Scrapingdog YouTube Comment API.

For this example, we are going to scrape data from this video.

Elon Musk Video Screen

Reason for selecting this video:

  • I wanted to analyze whether there’s genuine public interest in his new party.

  • The video had over 3 million views, so I assumed the comment volume would be high as well, making it a good sample for analysis.

The ID of this video is PKlkLX8oGoY as you can clearly see it in the video URL.

1import requests
2import openai
3
4api_key = "your-api-key"
5url = "https://api.scrapingdog.com/youtube/comments/"
6
7params = {
8 "api_key": api_key,
9 "v": "PKlkLX8oGoY"
10}
11
12response = requests.get(url, params=params)
13
14if response.status_code == 200:
15 data = response.json()
16 print(data)
17else:
18 print(f"Request failed with status code: {response.status_code}")

Once I run this code, I will get comments from the I page. Now, we will conclude our decision based on the comments from the first three pages only.

To get the comments from the next page, we will have to use the next_page_token parameter. You will get this token within the response of the I page.

1import requests
2import openai
3all_comments=[]
4api_key = "your-api-key"
5url = "https://api.scrapingdog.com/youtube/comments/"
6next_page_token=""
7for i in range(0,3):
8 params = {
9 "api_key": api_key,
10 "v": "PKlkLX8oGoY",
11 "next_page_token":next_page_token
12 }
13
14 response = requests.get(url, params=params)
15
16 if response.status_code == 200:
17 data = response.json()
18 for x in range(0,len(data['comments'])):
19 print(data['comments'][x]['text'])
20
21 all_comments.append(data['comments'][x]['text'])
22 next_page_token=data['pagination']['replies_next_page_token']
23 print(next_page_token)
24 else:
25 print(f"Request failed with status code: {response.status_code}")

So, once the loop is over, we will have all the comments from the first three pages inside the all_comments array.

Now, let’s pass these comments to the OpenAI API and see what the sentiments of citizens are.

Sentiment Analysis using OpenAI

1def analyze_sentiment(comments):
2 print("once")
3 prompt = "Classify the sentiment of each YouTube comment below as Positive, Negative, or Neutral. Return in the format: Comment → Sentiment.\n\n"
4 prompt += "\n".join([f"{c}" for c in comments])
5 ai_query = "Classify the sentiment of the people in 2 lines"
6
7
8 user_prompt = f"User Query: {ai_query}\n\nText:\n{prompt}"
9 system_prompt = "You are a sentiment analysis engine. Return a 2 line result whether people are positive or negative on elon's new party."
10
11 headers = {
12 "Authorization": f"Bearer {OPENROUTER_API_KEY}",
13 "Content-Type": "application/json",
14 "HTTP-Referer": "https://scrapingdog.com", # Mandatory for OpenRouter
15 "X-Title": "YouTube Comment Sentiment Analyzer"
16 }
17
18 payload = {
19 "model": "openai/gpt-4.1-nano",
20 "messages": [
21 {"role": "system", "content": system_prompt},
22 {"role": "user", "content": user_prompt}
23 ],
24 "temperature": 0.2
25 }
26
27 response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
28
29 if response.status_code == 200:
30 result = response.json()
31 return result["choices"][0]["message"]["content"].strip()
32 else:
33 print("❌ Error:", response.status_code, response.text)
34 return "Error"

Let me explain this code.

  • Builds a prompt by joining all YouTube comments line by line.

  • Prepares a user_prompt with the query "Classify the sentiment in 2 lines".

  • Adds a system_prompt telling the AI to summarize public sentiment on Elon's new party.

  • Sets required headers for OpenRouter (API key, Referer, X-Title).

  • Constructs the payload for the OpenRouter chat API using gpt-4.1-nano.

  • Sends a POST request to OpenRouter’s /chat/completions endpoint.

  • If successful, returns the 2-line AI-generated sentiment summary.

  • If it fails, it prints an error and returns "Error".

Once you run this code, you will get the sentiments of the American citizens on his new party.

Most comments express skepticism, criticism, or concern about Elon Musk's new party, indicating a predominantly negative sentiment. A few comments show curiosity or mild support, but overall, the tone is largely negative.

Complete Code

You can certainly scrape all the comments for a broader analysis, but for now, the code focuses on a limited set for demonstration purposes.

1import requests
2import openai
3
4OPENROUTER_API_KEY = 'your-key'
5
6all_comments=[]
7api_key = "your-key"
8url = "https://api.scrapingdog.com/youtube/comments/"
9next_page_token=""
10
11
12
13def analyze_sentiment(comments):
14 print("once")
15 prompt = "Classify the sentiment of each YouTube comment below as Positive, Negative, or Neutral. Return in the format: Comment → Sentiment.\n\n"
16 prompt += "\n".join([f"{c}" for c in comments])
17 ai_query = "Classify the sentiment of the people in 2 lines"
18
19
20 user_prompt = f"User Query: {ai_query}\n\nText:\n{prompt}"
21 system_prompt = "You are a sentiment analysis engine. Return a 2 line result whether people are positive or negative on elon's new party."
22
23 headers = {
24 "Authorization": f"Bearer {OPENROUTER_API_KEY}",
25 "Content-Type": "application/json",
26 "HTTP-Referer": "https://scrapingdog.com", # Mandatory for OpenRouter
27 "X-Title": "YouTube Comment Sentiment Analyzer"
28 }
29
30 payload = {
31 "model": "openai/gpt-4.1-nano",
32 "messages": [
33 {"role": "system", "content": system_prompt},
34 {"role": "user", "content": user_prompt}
35 ],
36 "temperature": 0.2
37 }
38
39 response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
40
41 if response.status_code == 200:
42 result = response.json()
43 return result["choices"][0]["message"]["content"].strip()
44 else:
45 print("❌ Error:", response.status_code, response.text)
46 return "Error"
47
48for i in range(0,3):
49 params = {
50 "api_key": api_key,
51 "v": "PKlkLX8oGoY",
52 "next_page_token":next_page_token
53 }
54
55 response = requests.get(url, params=params)
56
57 if response.status_code == 200:
58 data = response.json()
59 for x in range(0,len(data['comments'])):
60
61 all_comments.append(data['comments'][x]['text'])
62 next_page_token=data['pagination']['replies_next_page_token']
63 print(next_page_token)
64 else:
65 print(f"Request failed with status code: {response.status_code}")
66
67
68print(analyze_sentiment(all_comments))

Here are 5 Quick Takeaways:

  • You can scrape real YouTube comments at scale using Scrapingdog’s YouTube Comment API and Python.

  • The tutorial shows how to collect comments page by page using next_page_token to build a list of all comments.

  • Once scraped, comments are sent to an OpenAI model (GPT-4.1-nano) to generate a 1–2 line sentiment summary of overall public opinion.

  • This method gives you instant insights into viewers’ emotions toward a video topic e.g., positive, negative, or mixed sentiment.

  • Using an API-based approach means you don’t need browser automation, proxies, or manual parsing to analyze YouTube comments.

Conclusion

In this tutorial, we combined the power of Scrapingdog’s YouTube Comment API with OpenRouter’s GPT model to analyze real user sentiment at scale, all in Python.

By automating the process of collecting and classifying comments, you save hours of manual work and get instant insights into how audiences truly feel. Whether you’re tracking reactions to a product launch, political event, or viral video, this setup gives you a powerful edge.

And the best part? No browser automation or complex setup required.

Now, go ahead and plug in your own YouTube video, run the script, and see what the internet thinks.

Additional Resources

Try Scrapingdog for Free!

Get 200 free credits to spin the API. No credit card required!