如何使用YouTube API在YouTube搜索结果中获得更多结果?

时间:2018-10-24 07:03:26

标签: python youtube-data-api youtube-dl

我正在使用youtube-v3-api来获取搜索列表,通过使用“ nextPageToken”,我只能获得约500个结果,但是页面信息显示该结果包含超过200000个结果,如何获得更多结果?

def Search(sKeyWord):
"""Input search keyword, return search link list of youtube
Args:
    Input:
        sKeyWord: Search keyword
    Output:
        lVideos: A list of searched videos
"""
# gdr = googleapiclient.discovery.Resource
iMax = 50
gdrYoutube = build(
    YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    developerKey=DEVELOPER_KEY)

sPageToken = ""
lVideos = []
dictResponse = gdrYoutube.search().list(
    q=sKeyWord,
    part='id,snippet',
    maxResults=iMax,
    pageToken=sPageToken
    ).execute()

while(dictResponse.get("nextPageToken")):
    sPageToken = dictResponse["nextPageToken"]
    for dictSearchResult in dictResponse.get('items', []):
        if dictSearchResult['id']['kind'] == 'youtube#video':
            """
            lVideos.append(
                '%s (%s)' % (
                    dictSearchResult['snippet']['title'],
                    dictSearchResult['id']['videoId']))
            """
            lVideos.append(dictSearchResult['id']['videoId'])
    dictResponse = gdrYoutube.search().list(
        q=sKeyWord,
        part='id,snippet',
        maxResults=iMax,
        pageToken=sPageToken
        ).execute()

return lVideos

0 个答案:

没有答案