How to Extract YouTube Comments Using Youtube API - Python - GeeksforGeeks

26 Dec, 2020
Prerequisite: YouTube API
Google provides a large set of API’s for the developer to choose from. Each and every service provided by Google has an associated API. Being on of them, YouTube Data API is very simple to use provides features like –
Search for videos
Handle videos like retrieve information about a video, insert a video, delete a video etc.
Handle Subscriptions like lists all the subscriptions, insert or delete a subscription etc.
In this article, we will discuss How to Extract YouTube Comments and reply using Google YouTube API in Python.
Understand step by step implementation:-
Retrieve YouTube Video ResultsHere we will use commentThreads, list, execute method, it will give the list of comment and repliesInside list method, pass snippet and replies in part property and in videoId property pass video id of video URL
Here we will use commentThreads, list, execute method, it will give the list of comment and replies
Inside list method, pass snippet and replies in part property and in videoId property pass video id of video URL
Python3
# creating youtube resource objectyoutube = build('youtube','v3',                developerKey="Enter API Key")  # retrieve youtube video resultsvideo_response=youtube.commentThreads().list(  part='snippet,replies',  videoId="Enter Video ID").execute()
Iterate through each Video Response and fetch comments and repliesThe data comes in dictionary format, each comment data has reply count number, if reply count number is zero means no reply on that commentif count is greater than zero then we are iterating each reply and get text.nextPageToken contain the next data, here we are checking if nextPageToken has no value it means value is None, loop end, else loop will continue.
The data comes in dictionary format, each comment data has reply count number, if reply count number is zero means no reply on that comment
if count is greater than zero then we are iterating each reply and get text.
nextPageToken contain the next data, here we are checking if nextPageToken has no value it means value is None, loop end, else loop will continue.
Below is the full implementation:
Python3
from googleapiclient.discovery import build  api_key = 'API KEY'  def video_comments(video_id):    # empty list for storing reply    replies = []      # creating youtube resource object    youtube = build('youtube', 'v3',                    developerKey=api_key)      # retrieve youtube video results    video_response=youtube.commentThreads().list(    part='snippet,replies',    videoId=video_id    ).execute()      # iterate video response    while video_response:                # extracting required info        # from each result object         for item in video_response['items']:                        # Extracting comments            comment = item['snippet']['topLevelComment']['snippet']['textDisplay']                          # counting number of reply of comment            replycount = item['snippet']['totalReplyCount']              # if reply is there            if replycount>0:                                # iterate through all reply                for reply in item['replies']['comments']:                                        # Extract reply                    reply = reply['snippet']['textDisplay']                                          # Store reply is list                    replies.append(reply)              # print comment with list of reply            print(comment, replies, end = '\n\n')              # empty reply list            replies = []          # Again repeat        if 'nextPageToken' in video_response:            video_response = youtube.commentThreads().list(                    part = 'snippet,replies',                    videoId = video_id                ).execute()        else:            break  # Enter video idvideo_id = "Enter Video ID"  # Call functionvideo_comments(video_id)
Output:
Let’s verify the results:
3 Comments and 2 Replies
Picked
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Pandas dataframe.groupby()
Create a directory in Python
Defaultdict in Python
Python | Get unique values from a list