我正在从 Youtube 频道收集有关视频的信息,该频道的视频数量 > 50。
所以这意味着我需要发出几个请求,因为每个 JSON 响应的最大结果是 50 个视频。
我找到了一些解决方案,现在代码看起来像这样
videoMetadata = [] #declaring our list, where the results will be stored
# First request
url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId='+CHANNEL_ID+'&maxResults=50&type=video&key='+API_KEY
response = urllib.request.urlopen(url) #makes the call to YouTube
videos = json.load(response) #decodes the response so we can work with it
nextPageToken = videos.get("nextPageToken") #gets the token of next page
# Retrieve all the rest of the pages
while nextPageToken:
url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId='+CHANNEL_ID+'&maxResults=50&type=video&key='+API_KEY+"&pageToken="+nextPageToken
response = urllib.request.urlopen(url)
videos_next_page = json.load(response)
nextPageToken = videos_next_page.get("nextPageToken")
# loops through results and appends it to videoMetadata list
# loop for the first page
for video in videos['items']:
if video['id']['kind'] == 'youtube#video':
videoMetadata.append(video['id']['videoId'])
# loop for the next page
for video in videos_next_page['items']:
if video['id']['kind'] == 'youtube#video':
它工作正常,但也许有更好的解决方案,我如何将多个 JSON 响应的结果存储在列表中?
将不胜感激任何建议。
慕雪6442864
相关分类