Home > front end >  How to paginate search with YouTube Data Api on Python?
How to paginate search with YouTube Data Api on Python?

Time:08-21

I need to analyze all id's from search and in order 50 maxResults is not relevant for me, im trying to increase number of retrieved data by pagination. I want to understand how to do that.

Here is how my code look like:

api_key = "***************"

from googleapiclient.discovery import build
youtube = build('youtube','v3',developerKey = api_key)
print(type(youtube))

request = youtube.search().list(
    q='my unique search query',
    part='id',
    maxResults=50,
    order="viewCount",
    pageToken="CAoQAA",
    type='video')


print(type(request))
res = request.execute()
from pprint import PrettyPrinter
pp = PrettyPrinter()
pp.pprint(res)

CodePudding user response:

According to documentation about pagination, you need to loop your requests to YouTube Data API v3 Search: list endpoint by providing as pageToken the retrieved nextPageToken if there is any. So your code becomes:

api_key = "***************"

from googleapiclient.discovery import build
from pprint import PrettyPrinter
youtube = build('youtube','v3',developerKey = api_key)
print(type(youtube))

pp = PrettyPrinter()
nextPageToken = ''

while True:
    request = youtube.search().list(
        q='my unique search query',
        part='id',
        maxResults=50,
        order="viewCount",
        pageToken=nextPageToken,
        type='video')
    
    print(type(request))
    res = request.execute()
    pp.pprint(res)
    
    if 'nextPageToken' in res:
        nextPageToken = res['nextPageToken']
    else:
        break
  • Related