Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Google Youtube API for scraping all comments from youtube video

$
0
0

I am trying to fetch all the comments along with replies from the below youtube video linkhttps://www.youtube.com/watch?v=kqtD5dpn9C8 using the youtube API in python.However, the code I am using is going in an infinite loop repeating same sets of comments. I am new in this stuff and seeking for sincere help. The code I am using is given below. TIA.

def video_comments(video_id):li = []# empty list for storing replyreplies = []# creating youtube resource objectyoutube = build('youtube', 'v3',                developerKey=api_key)# retrieve youtube video resultsvideo_response=youtube.commentThreads().list(part='snippet,replies',videoId=video_id).execute()# iterate video responsewhile video_response:    # extracting required info    # from each result object     for item in video_response['items']:        # Extracting comments        comment = item['snippet']['topLevelComment']['snippet']['textDisplay']        li.append(comment)          # 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')        li.append(replies)        # empty reply list        replies = []    # Again repeat    if 'nextPageToken' in video_response:        video_response = youtube.commentThreads().list(                part = 'snippet,replies',                videoId = video_id                       ).execute()    else:        breakreturn(li)

Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>