检索到的所有结果页均未涵盖全部结果

时间:2019-07-09 12:37:29

标签: python youtube-api

我正在使用YouTube数据API从经过身份验证的用户帐户中检索所有喜欢的视频。

我的帐户中喜欢的视频总数超过3000。我正在使用nextPageToken转到下一页。但是,我最多只能浏览20页。由于我设置了maxResults = 50,因此总共有1000个视频,这对我来说似乎很具体。我不明白为什么即使没有覆盖3000个视频,第20页上也没有nextPageToken,是否还有另一个限制,那就是您不能检索超过1000个视频?

我编写了以下代码,将每个类别的视频计数存储在字典中。类别ID 10对应于音乐,类别20对应于游戏,依此类推。

request  = youtube.videos().list(part = 'snippet', myRating = 'like', maxResults = 50)
likedVideos = request.execute()
print(likedVideos['pageInfo']) # prints {'totalResults': 3398, 'resultsPerPage': 50}

countByCategoryId = {}

for video in likedVideos['items']:
    catId = video['snippet']['categoryId']
    if catId in countByCategoryId:
        countByCategoryId[catId] += 1
    else:
        countByCategoryId[catId] = 1

count = 1

while 'nextPageToken' in likedVideos.keys():
    count += 1
    print('page', count) # prints numbers from 2 to 20
    print(likedVideos['pageInfo']) # prints {'totalResults': 3398, 'resultsPerPage': 50} in every iteration

    request  = youtube.videos().list(part = 'snippet', myRating = 'like', maxResults = 50, pageToken = likedVideos['nextPageToken'])
    likedVideos = request.execute()
    for video in likedVideos['items']:
        catId = video['snippet']['categoryId']
        if catId in countByCategoryId:
            countByCategoryId[catId] += 1
        else:
            countByCategoryId[catId] = 1

print('nextPageToken' in likedVideos.keys()) # prints False
print(sum(countByCategoryId.values()) # prints 974, less than 1000 because some videos were deleted or made private

0 个答案:

没有答案