youtube api的计数与检索的项目数不匹配

时间:2017-01-05 11:09:24

标签: python-3.x youtube-api

youtube的播放列表项数似乎与播放列表中的实际项目数不匹配。这有什么理由还是我做错了什么?

我的代码返回以下内容:

there are 1275 videos in the playlist 
we retrieved 1274 videos

代码,将您的密钥用于使用

import requests
import json

key = '' #Put your key here
upload_playlist_id = 'UUG-Et3jfinzlQql4uEYjAVw'

# Get the video count from the content details of the playlist
params = {
    'key' : key,
    'id' : upload_playlist_id,
    'part' : 'contentDetails'
}

response = requests.get(r'https://www.googleapis.com/youtube/v3/playlists', params)
data = json.loads(response.text)
video_count = data['items'][0]['contentDetails']['itemCount']
print("there are %s videos in the playlist " % (video_count))

# Get the video count from counting all of the items in the playlist
params = {
    'key' : key,
    'playlistId' : upload_playlist_id,
    'maxResults' : 50,
    'part' : 'id'
}
response = requests.get(r'https://www.googleapis.com/youtube/v3/playlistItems', params)
data_list = [json.loads(response.text)]

while 'nextPageToken' in data_list[-1]:
    params['pageToken'] = data_list[-1]['nextPageToken']
    response = requests.get(r'https://www.googleapis.com/youtube/v3/playlistItems', params)
    data_list.append(json.loads(response.text))

video_count = sum([len(page_data['items']) for page_data in data_list])
print("we retrieved %s videos" % (video_count))

1 个答案:

答案 0 :(得分:0)

我尝试使用Playlists: list获取您提供的playlistId中的视频总数,并得到1275个视频的结果。

以下是我用于获取playlistId " UUG-Et3jfinzlQql4uEYjAVw"

的视频总数的查询

检查"itemCount"参数以检查视频总数。

https://www.googleapis.com/youtube/v3/playlists?part=contentDetails&id=UUG-Et3jfinzlQql4uEYjAVw&key=YOUR_API_KEY