【问题标题】:Python YouTube API Retrieve List of VideosPython YouTube API 检索视频列表
【发布时间】:2018-10-27 00:09:00
【问题描述】:

我想使用 Python 从 YouTube API 获取给定 ChannelIdall 视频。下面是我尝试过的代码,但我不知道如何在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!")

【问题讨论】:

    标签: python json youtube-api


    【解决方案1】:
    json_data.update(r.json())
    

    应该做的伎俩。

    【讨论】:

      猜你喜欢
      • 2020-04-24
      • 2013-07-08
      • 2012-04-01
      • 2012-09-28
      • 2013-09-19
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 2016-06-15
      相关资源
      最近更新 更多