Do Hun Kim
Do Hun Kim

Reputation: 125

YouTube Data API how to extract more than 100 comments?

I am using YouTube Data API v3 to extract comments but the maximum I can get is 100 comments.

This is the code

url = "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&maxResults=1000&order=relevance&videoId=cosc1UEbMbY&key=MYKEY"

data = requests.get(url).json()

comments = []
co = 0;

for i in range(len(data['items'])):
  comments.append(data['items'][i]['snippet']['topLevelComment']['snippet']['textOriginal'])

Even if I set maxResults parameter to 1000, it only returns 100 comments. How can I fix this problem?

Thank you

Upvotes: 2

Views: 2321

Answers (1)

stuckoverflow
stuckoverflow

Reputation: 712

There is a key named "nextPageToken" in the JSON response, it can be used in the next request.

Heres a sample code snippet using the google-api-client for Python :

if 'nextPageToken' in data:
    new_data = youtube.playlistItems().list(
    part='snippet',
    maxResults=100,
    pageToken=data['nextPageToken']
    )
    data= new_data.execute()
    # application code

else:
     pass

Upvotes: 2

Related Questions