How to Extract YouTube Comments in Python using YouTube API?

Posted in /  

How to Extract YouTube Comments in Python using YouTube API?
vinaykhatri

Vinay Khatri
Last updated on November 24, 2024

    Extracting information from the web is pretty cool, and it becomes more professional when you use APIs provided by a specific vendor or Application. Youtube is the most popular video-sharing platform, and it is owned by the world's biggest tech giant Google.

    Although Extracting data from Youtube can be performed with the help of Web Scraping. But web scraping is slow, and sometimes big web applications do not promote web scrapping, and they also might block your request considering you as a bot.

    Luckily Google provides many APIs to the developers, so they can interact with the Google suite applications and extract data.

    Here in this Python tutorial, we will be using the Google Youtube APIs and extracting the top 10 comments from the most-watched Youtube video, " Luis Fonsi - Despacito ft. Daddy Yankee".

    How to Extract YouTube Comments in Python using YouTube API?

    This Python tutorial is divided into 4 parts.

    1. Set up Youtube API and get API credentials.
    2. Get the Youtube Video ID.
    3. Install the Required Python Libraries.
    4. Write the Python Program to extract comments from the Youtube video.

    If you only wish to see the Python code, you can directly Jump to the 4th step. Else you can read the complete article and know how to set up API, Youtube Video ID, and what Python library you need to Install.

    Step 1: Setup Youtube API and Get Credentials

    When It's come to APIs provided by Google, you can set a project and enable the API for that specific project and generate credentials like an API key. The first step toward setting the project is, you need to visit the Google API Dashboard , which is provided by Google for the developers.

    When you use the Google API Dashboard or Google Developer Console, it goes without saying that you should have logged in to the system with a Google or Gmail account.

    Click here to visit the Google API Dashboard. And you will see a similar screen.

    Here You need to click on the Drop Down option, Select a project and click on the New PROJECT button to create a new Project.

    Now give the name of your project. For this tutorial, I have specified my project name "MyFirstYoutubeAPI" . After writing the project name, click on the CREATE button. This will create a new project for you, now you need to select the Project from the Select a Project option.

    After successfully creating and selecting the project, now you need to search for the YouTube Data API v3.

    Now ENABLE the API.

    Now the Youtube Data API v3 has been enabled for your project, now you need to create the Credentials, for that, click on the Credentials Option, and click on the +CREATE CREDENTIALS button.

    When you click on the CREATE CREDENTIALS button, you will get three options. Among those three options, you need to click on API KEY.

    When you click on the API KEY a window will pop up with the API Key, you need to copy that because we will be using that key in our Python program.

    After Successfully copying the API Key on your keyboard, click on the close button.

    Note: Never share your API key with anyone.

    Now we have our API key, so here the Step 1 Complete.

    Step 2: Get the Youtube Video ID

    Now we need to get the Youtube Video ID. For this tutorial, we will be extracting comments from the  " Luis Fonsi - Despacito ft. Daddy Yankee ," so we require its video id. It's very easy to get the video ID of any video present on Youtube, you can simply get the video ID from its URL.

    To get the ID, we need to check the video Page URL "https://www.youtube.com/watch?v= kJQP7kiw5Fk " The characters after the v= represent the video Id. So the videoID of '" Luis Fonsi - Despacito ft. Daddy Yankee" is " kJQP7kiw5Fk" Now we also have the video ID of the youtube video which comments we want to extract. Now here, our second step ends.

    Step 3: Install the Required Python Libraries

    Now we need to install the required library, which we help us to connect our Python program to connect with youtube and extract data. Whenever we use any Google API in Python, it's always a good practice to use the google-api-python-client library instead of requests.

    The google-api-python-client library is provided by Google itself so the Python developer can interact with Google APIs. Run the following pip install command on your terminal to install google-api-python-client library.

    pip install google-api-python-client

    By we are all set, now it's time to write the Python code. Open your favorite Python IDE and Text editor and start coding with me.

    Python Implementation

    Let's first import the required Library and specify the credentials.

    from googleapiclient.discovery import build
    
    #credentials https://console.developers.google.com/
    
    api_key ="AIzaSyDMXwwsjndhdbsnsjgidkmehsVCK9uJkA"
    video_id= "kJQP7kiw5Fk"

    Now build a Google resource for Youtube API.

    #build a resource for youtube
    resource = build('youtube', 'v3', developerKey=api_key)

    The build() method will build a resource for the youtube API version 3. Now create a request using the resource object to fetch the comments from the specific Youtube Video.

    #create a request to get 20 comments on the video
    request = resource. commentThreads().list(
                                part="snippet",
                                videoId=video_id,
                                maxResults= 20,   #get 20 comments
                                order="orderUnspecified")  #top comments.

    Here, we have used the build object using resource commentThreads() method. The comment thread method will fetch the maxResults number of comments on a specific video. now to get the response, we need to execute the request. To execute the request, we will call the execute() method on the request object.

    #execute the request
    response =request.execute()

    The response is a list of 20 comments on the specified video. But in this tutorial, we will only be extracting the top 10 comments, so now, let's get only 10 items from the response object.

    #get first 10 items from 20 comments 
    items = response["items"][:10]

    Now we have the top 10 items from the response object now let's loop through those items and extract every comment text, its publish date, number of likes on the comments, and the comment user.

    print("------------------------------------------------------------------------------------------------------")
    for item in items:
        item_info = item["snippet"]
        
        #the top level comment can have sub reply comments
        topLevelComment = item_info["topLevelComment"]
        comment_info = topLevelComment["snippet"]
        
        print("Comment By:", comment_info["authorDisplayName"])
        print("Comment Text:" ,comment_info["textDisplay"])
        print("Likes on Comment:", comment_info["likeCount"])
        print("Comment Date: ", comment_info['publishedAt'])
        print("================================\n")

    Now put all the code together and execute

    #Python program to extract comments from Youtube using Youtube API.

    from googleapiclient.discovery import build
    
    #credentials 
    api_key ="AIzaSyDMXwwsjndhdbsnsjgidkmehsVCK9uJkA"
    video_id= "kJQP7kiw5Fk"
    
    #build a resource for youtube
    resource = build('youtube', 'v3', developerKey=api_key)
    
    #create a request to get 20 comments on the video
    request = resource. commentThreads().list(
                                part="snippet",
                                videoId=video_id,
                                maxResults= 20,   #get 20 comments
                                order="orderUnspecified")  #top comments.
    #execute the request
    response =request.execute()
    
    #get first 10 items for from 20 comments 
    items = response["items"][:10]
    
    print("------------------------------------------------------------------------------------------------------")
    for item in items:
        item_info = item["snippet"]
        
        #the top level comment can have sub reply comments
        topLevelComment = item_info["topLevelComment"]
        comment_info = topLevelComment["snippet"]
        
        print("Comment By:", comment_info["authorDisplayName"])
        print("Coment Text:" ,comment_info["textDisplay"])
        print("Likes on Comment :", comment_info["likeCount"])
        print("Comment Date: ", comment_info['publishedAt'])
        print("================================\n")

    Output

    ------------------------------------------------------------------------------------------------------
    Comment By: cruisendude
    Comment Text: The first viewer must feel like the king of the world...
    Likes on Comment: 86399
    Comment Date: 2020-08-21T23:15:01Z
    ================================
    
    Comment By: Hazzard
    Comment Text: Man, you are wonderful channel
    Likes on Comment: 509
    Comment Date: 2021-01-25T22:32:12Z
    ================================
    
    Comment By: Mr_Saiiko
    Comment Text: me reading the comments be like: ""“Minecraft" "asmr" "pewdiepie" "music" "fortnite" "markiplier" “YouTube is a perfectly balanced game with no exploits.” "Runescape" "World of Warcraft" "Shadowlands" "Dream" "MrBeast" "Warzone" "FaZe Clan" "100 Thieves" "Call of Duty" "Pokemon" "Pokemon cards" "card unboxing" "charizard" "they don't want you to know" "Flat earth" "round earth" "triangle earth" "earth is not earth" "what even is earth if not earth omg government is lying to you" "minecraft" "asmr" "pewdiepie" "music" "fortnite" "markiplier" “YouTube is a perfectly balanced game with no exploits.” "Runescape" "World of Warcraft" "Shadowlands" "Dream" "MrBeast" "Warzone" "FaZe Clan" "100 Thieves" "Call of Duty" "Pokemon" "Halo" "Devil may cry" “YouTube is a perfectly balanced game with no exploits.” “Cocomelon” “t series” “Minecraft" "asmr" "pewdiepie" "music" "fortnite" "markiplier" “YouTube is a perfectly balanced game with no exploits.” "Runescape" "World of Warcraft" "Shadowlands" "Dream" "MrBeast" "Warzone" "FaZe Clan" "100 Thieves" "Call of Duty" "Pokemon" "Pokemon cards" "card unboxing" "charizard" "they don't want you to know" "Flat earth" "round earth" "triangle earth" "earth is not earth" "what even is earth if not earth omg government is lying to you" "minecraft" "asmr" "pewdiepie" "music" "fortnite" "markiplier" “YouTube is a perfectly balanced game with no exploits.” "Runescape" "World of Warcraft" "Shadowlands" "Dream" "MrBeast" "Warzone" "FaZe Clan" "100 Thieves" "Call of Duty" "Pokemon" "Halo" "Devil may cry" “YouTube is a perfectly balanced game with no exploits.” “Cocomelon” “t series”
    Likes on Comment: 83
    Comment Date: 2021-01-26T18:48:00Z
    ================================
    
    Comment By: Wade Coleman
    Comment Text: I hated when people tried to sing the lyrics and were all like<br />“Despacito”<br />“All I want is a burrrito”
    Likes on Comment: 10
    Comment Date: 2021-01-27T04:33:03Z
    ================================
    
    Comment By: Wassxm
    Comment Text: Teacher : What is the world’s population<br />Me : Around one despacito
    Likes on Comment: 42
    Comment Date: 2021-01-25T18:27:03Z
    ================================
    
    Comment By: Aishath Saila
    Comment Text: No-one:<br />Literally no-one:<br />Not even world itself:<br />Me: despacito ndyhwu jwbjabjqbjqbj
    Likes on Comment: 330
    Comment Date: 2021-01-25T13:31:38Z
    ================================
    
    Comment By: Fernando Nancabil
    Comment Text: Yo buscando un comentario en español ?
    Likes on Comment: 85
    Comment Date: 2021-01-25T19:06:24Z
    ================================
    
    Comment By: Noah- Brawl Stars
    Comment Text: nobody:<br />secretly everyone: Just checks the comments
    Likes on Comment: 263
    Comment Date: 2021-01-25T20:35:12Z
    ================================
    
    Comment By: vMuffin IOS
    Comment Text: Random Singer: Sings in spanish<br /><br />ALMOST ALL OF THE FUCKING WORLD : interesting
    Likes on Comment: 20
    Comment Date: 2021-01-26T12:01:44Z
    ================================
    
    Comment By: Anshu Soren
    Comment Text: Who just came to check the views?
    Likes on Comment: 343059
    Comment Date: 2019-04-27T06:58:42Z
    ================================

    Conclusion

    In this Python tutorial, you learned How to Extract YouTube Comments in Python using Google Youtube API and Python google-api-python-client library. In this Python tutorial, we have only extracted the youtube video comments, but the Youtube API provides many methods that can be used to extract much data from Youtube, which might be impossible with web-scrapping.

    I would recommend you to go through the Amazing documentation provided by Google Youtube on its Youtube API .

    People are also reading:

    Leave a Comment on this Post

    0 Comments