Python YouTube API检索视频列表

时间:2018-05-16 20:22:10

标签: python json youtube-api

我想使用Python从给定all的YouTube API中获取ChannelId个视频。下面是我尝试过的代码,但我无法弄清楚如何在while循环中附加数据。

import requests
import json

mykey = 'myGoogleKey'
channelid = 'someRandomChannelId'

# First request
r = requests.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId="+channelid+"&order=date&key="+mykey)
json_data = r.json()
nextPageToken = json_data.get("nextPageToken")

# Retrieve all the rest of the pages
while nextPageToken:
    r = requests.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId="+channelid+"&order=date&key="+mykey+"&pageToken="+nextPageToken)
    json_data.append(r.json()) # this part needs to be modified/adjusted
    nextPageToken = json_data.get("nextPageToken")

with open('myJsonFile.json', 'w') as outfile:
    json.dump(json_data, outfile, sort_keys=True, indent=4)

print("All Done!")

1 个答案:

答案 0 :(得分:0)

json_data.update(r.json())

应该做的伎俩。