我正在从YouTube channel 中收集有关视频的信息,该视频的数量大于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响应的结果存储在列表中?
将不胜感激任何建议。
最佳答案
实际上,除非您只有一个“下一页”,否则不会这样:
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")
每次迭代都会覆盖
videos_next_page
,因此您只会得到最后一页。一旦取消序列化,“JSON响应的结果”就是普通的普通python对象(通常是
dict
)。然后将它们添加到列表中,就像处理其他任何事情一样。这是一种可能的重写,可以正确处理此问题(并且也可以更好地利用内存)-警告:未经测试的代码,因此我不能保证没有任何错字或其他任何错误,但至少您可以理解。
def load_page(page_token=None):
url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={}&maxResults=50&type=video&key={}".format(CHANNEL_ID, API_KEY)
if page_token:
url += ("&pageToken={}".format(page_token))
response = urllib.request.urlopen(url) #makes the call to YouTube
return json.load(response)
def collect_videos_meta(page):
return [video['id']['videoId'] for video in page['items'] if video['id']['kind'] == 'youtube#video']
def main():
videoMetadata = []
nextPageToken = None # default initial value for the first page
# using `while True` and `break` avoids having to repeat the same
# code twice (once before the loop and once within the loop).
# This is a very common pattern in Python, you just have to make
# sure you will break out of the loop at some point...
while True:
page = load_page(nextPageToken)
videoMetadata.extend(collect_videos_meta(page))
nextPageToken = page.get("nextPageToken")
if not nextPageToken:
break
# now do what you want with those data...
print(videoMetadata)
if __name__ = "__main__":
main()
关于python - 如何通过YouTube JSON响应有效地分页?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57768954/