pip install googleapiclient
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
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 id video_id = "Enter Video ID" # Call function video_comments(video_id) |
Here you need to replace the api_key
of your project