Python将变量输出拆分为多个变量



首先,我为我的无知道歉,因为我是Python的新手,我可能会说一些没有意义的话。

我使用youtube.search.list API来创建变量search_response。这个变量以JSON格式输出所有搜索视频的数据(我相信它是JSON格式)。

搜索API限制为每页50个结果,但我已经能够使用分页返回多达550个结果(这对于我正在做的事情已经足够了)。

search.list只向我提供视频标题、发布日期、视频ID等详细信息。但是,我希望也能访问每个视频的观看次数、喜欢次数和不喜欢次数。

使用videos.list API,我已经能够拉入这些变量(视图,喜欢,不喜欢),但它似乎被限制在50个结果,也没有提供分页选项。

这是一个链接到Jupyter Notebook中的文件(也附加在.py中)。

所以我在想,如果我能把JSON文件(search_response)分成50个帖子的片段,我应该运行它10次,并下载所有视频的视图,喜欢和不喜欢。但是,我不知道如何分离我的search_response变量的输出,并感谢对此的任何想法或建议!

总结一下:

  • 我有一个变量search_response输出,JSON格式,数百个单独的部分(每个视频一个)
  • videos.list API收集详细的统计数据(视图,dis/like计数)有50个请求的限制
  • 我正在寻找一种方法将search_response输出分离为50个部分的多个变量,每个部分在API中单独运行

代码: 收集数据

from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
DEVELOPER_KEY = "REPLACE ME"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
def fetch_all_youtube_videos(channelId):
youtube = build(YOUTUBE_API_SERVICE_NAME,
                YOUTUBE_API_VERSION,
                developerKey=DEVELOPER_KEY)
res = youtube.search().list(
q="",
part="id,snippet",
channelId=channelId,
maxResults=50,
).execute()
nextPageToken = res.get('nextPageToken')
while ('nextPageToken' in res):
    nextPage = youtube.search().list(
    part="id,snippet",
    channelId=channelId,
    maxResults="50",
    pageToken=nextPageToken
    ).execute()
    res['items'] = res['items'] + nextPage['items']
    if 'nextPageToken' not in nextPage:
        res.pop('nextPageToken', None)
    else:
        nextPageToken = nextPage['nextPageToken']
return res
if __name__ == '__main__':
search_response = fetch_all_youtube_videos("UCp0hYYBW6IMayGgR-WeoCvQ")
videos = []   
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
    videos.append("%s (%s)" % (search_result["snippet"]["title"],
                             search_result["id"]["videoId"]))

代码:添加详细统计信息

youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
videos = {}
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
    videos[search_result["id"]["videoId"]] = search_result["snippet"]["title"]
video_ids_list = ','.join(videos.keys())
video_list_stats = youtube.videos().list(
 id=video_ids_list,
 part='id,statistics'
).execute()

理想输出:该输出对应于 search_response 变量的前50个etag部分(由于本网站的字符限制,这里只显示25个左右,但应该是50个部分),可以称为 search_response1 ,其中 search_response2 可以包含etag部分51-100,等等。

    search_response1 = {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/hsQmFEqp1R_glFpcQnpnOLbbxCg"',
   'id': {'kind': 'youtube#video', 'videoId': 'd8kCTPPwfpM'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "This incredible duo teamed up to perform an original song for Ellen! They may not have had a lot of rehearsal, but it's clear that this is one musical combo it ...",
    'liveBroadcastContent': 'none',
    'publishedAt': '2012-02-21T14:00:00.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/d8kCTPPwfpM/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/d8kCTPPwfpM/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/d8kCTPPwfpM/mqdefault.jpg',
      'width': 320}},
    'title': 'Taylor Swift and Zac Efron Sing a Duet!'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/LeKypRrnWWD6mRhK1wATZB5UQGo"',
   'id': {'kind': 'youtube#video', 'videoId': '-l2KPjQ2lJA'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "Harry, Liam, Louis and Niall played a round of Ellen's revealing game. How well do you know the guys of One Direction?",
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-11-18T14:00:00.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/-l2KPjQ2lJA/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/-l2KPjQ2lJA/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/-l2KPjQ2lJA/mqdefault.jpg',
      'width': 320}},
    'title': 'Never Have I Ever with One Direction'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/qm7jX3gngQBYS7xv9sROxTpUtDU"',
   'id': {'kind': 'youtube#video', 'videoId': 'Jr7bRw0NxQ4'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'Ellen has always loved giving her guests a good thrill, and she put together this montage of some of her favorite scares from over the years!',
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-11-19T14:00:00.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/Jr7bRw0NxQ4/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/Jr7bRw0NxQ4/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/Jr7bRw0NxQ4/mqdefault.jpg',
      'width': 320}},
    'title': "Ellen's Never-Ending Scares"}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/dWDEID-z2CI4P-eh62pmTxWq0uc"',
   'id': {'kind': 'youtube#video', 'videoId': 't5jw3T3Jy70'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "Kristen Bell loves sloths. You might even say she's obsessed with them. She told Ellen about what happened when her boyfriend, Dax Shepard, introduced her ...",
    'liveBroadcastContent': 'none',
    'publishedAt': '2012-01-31T03:18:55.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/t5jw3T3Jy70/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/t5jw3T3Jy70/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/t5jw3T3Jy70/mqdefault.jpg',
      'width': 320}},
    'title': "Kristen Bell's Sloth Meltdown"}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/ZSOfmz-dGZ3R0LNJ2n1LLQ4hEjg"',
   'id': {'kind': 'youtube#video', 'videoId': 'fC_Z5HlK9Pw'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "The two incredibly handsome and talented stars got hilariously honest while playing one of Ellen's favorite games.",
    'liveBroadcastContent': 'none',
    'publishedAt': '2016-05-18T13:00:04.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/fC_Z5HlK9Pw/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/fC_Z5HlK9Pw/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/fC_Z5HlK9Pw/mqdefault.jpg',
      'width': 320}},
    'title': 'Drake and Jared Leto Play Never Havexa0Ixa0Ever'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/M9siwkGRaHrf5ELg2R1JceH2KmA"',
   'id': {'kind': 'youtube#video', 'videoId': '4aKteL3vMvU'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'The amazing Adele belted out her hit song for the first time since her Grammy performance.',
    'liveBroadcastContent': 'none',
    'publishedAt': '2016-02-18T14:00:00.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/4aKteL3vMvU/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/4aKteL3vMvU/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/4aKteL3vMvU/mqdefault.jpg',
      'width': 320}},
    'title': "Adele Performsxa0'Allxa0Ixa0Ask'"}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/od24uJeVxDWErhRWWtsSKHuD9oQ"',
   'id': {'kind': 'youtube#video', 'videoId': 'WOgKIlvjlQ8'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'Ellen, Johnny Depp, Gwyneth Paltrow and Paul Bettany all played an incredibly revealing round of "Never Have I Ever." You won't believe what they revealed!',
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-01-23T14:00:13.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/WOgKIlvjlQ8/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/WOgKIlvjlQ8/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/WOgKIlvjlQ8/mqdefault.jpg',
      'width': 320}},
    'title': 'Never Have I Ever'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/Wu5vxAyQ5QGl6uO7eIodYHjaqVI"',
   'id': {'kind': 'youtube#video', 'videoId': '07nXzFPHiGU'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "The two music icons played a revealing game with Ellen. You won't believe their responses.",
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-03-19T13:00:00.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/07nXzFPHiGU/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/07nXzFPHiGU/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/07nXzFPHiGU/mqdefault.jpg',
      'width': 320}},
    'title': 'Never Have I Ever with Madonna and Justin Bieber'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/Z8HWp7YAzQPWcodFthhbxOU3l2U"',
   'id': {'kind': 'youtube#video', 'videoId': 'Wh8m4PYlSGY'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'Ellen challenged Jennifer Lopez to a round of her fun and revealing game.',
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-05-18T19:00:01.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/Wh8m4PYlSGY/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/Wh8m4PYlSGY/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/Wh8m4PYlSGY/mqdefault.jpg',
      'width': 320}},
    'title': 'J.Lo and Ellen Play Never Have I Ever'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/3B4UvmeCI4Y9UZC2f6kF09wpmW8"',
   'id': {'kind': 'youtube#video', 'videoId': 'QJY5VVQsFZ0'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'Adele sure can sing, but can she beat the clock? Watch what happened when Ellen challenged her to a game of 5 Second Rule!',
    'liveBroadcastContent': 'none',
    'publishedAt': '2016-02-24T14:00:01.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/QJY5VVQsFZ0/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/QJY5VVQsFZ0/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/QJY5VVQsFZ0/mqdefault.jpg',
      'width': 320}},
    'title': '5 Second Rule with Adele'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/mYDa0rUVIvzHCbQfJGRq2VeZ2so"',
   'id': {'kind': 'youtube#video', 'videoId': 'vEVrYx8-lys'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': '"Brave" may be the wrong word, but Ellen's Executive Producer, Andy Lassner, and his assistant, Jacqueline, made their way through a haunted house.',
    'liveBroadcastContent': 'none',
    'publishedAt': '2014-10-31T16:50:33.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/vEVrYx8-lys/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/vEVrYx8-lys/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/vEVrYx8-lys/mqdefault.jpg',
      'width': 320}},
    'title': 'Andy and Jacqueline Brave the Haunted House'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/kprvuH9aAXC-dIAlXMbwA3Klp14"',
   'id': {'kind': 'youtube#video', 'videoId': 'wTAJSuhgZxA'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "Adele's new single is a hit, thanks to a chat she had with Ellen…",
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-10-29T13:00:01.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/wTAJSuhgZxA/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/wTAJSuhgZxA/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/wTAJSuhgZxA/mqdefault.jpg',
      'width': 320}},
    'title': "Ellen Inspired Adele's New Song"}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/Ge10B_3x9KSfQTWF5V-ZNHDuqwU"',
   'id': {'kind': 'youtube#video', 'videoId': 'oJsYwehp_r4'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'She loves to scare her guests. Take a look at a few of these recent favorites!',
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-05-27T13:00:01.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/oJsYwehp_r4/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/oJsYwehp_r4/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/oJsYwehp_r4/mqdefault.jpg',
      'width': 320}},
    'title': "Ellen's Favorite Scares"}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/Em-6euVf5saohkrtNZzXR2jmaTo"',
   'id': {'kind': 'youtube#video', 'videoId': 'Vap9SMRf8YE'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "Ellen and Sofia played a hilarious game of 5 Second Rule! Check it out, plus all the fun we didn't have time for in the show!",
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-12-03T14:00:01.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/Vap9SMRf8YE/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/Vap9SMRf8YE/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/Vap9SMRf8YE/mqdefault.jpg',
      'width': 320}},
    'title': '5 Second Rule with Sofia Vergara -- Extended!'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/o7GMiOOo84bHhNwHGc6qQZ1ebRc"',
   'id': {'kind': 'youtube#video', 'videoId': 'ZXZ6K21wvZM'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "Ellen's writer Amy scares pretty easily, but she's nothing compared to Ellen's Executive Producer, Andy. That's why he was the perfect person to join Amy in this ...",
    'liveBroadcastContent': 'none',
    'publishedAt': '2013-10-22T13:00:05.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/ZXZ6K21wvZM/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/ZXZ6K21wvZM/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/ZXZ6K21wvZM/mqdefault.jpg',
      'width': 320}},
    'title': "Andy and Amy's Haunted House"}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/SyuA5bIoXdtQD_0SQUu1PyfvPP4"',
   'id': {'kind': 'youtube#video', 'videoId': 'QrIrbeoDkT0'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "Ellen met Noah Ritter after a video of him went viral. Nobody could have predicted what she was in for. Ellen Meets the 'Apparently' Kid, Part 2 ...",
    'liveBroadcastContent': 'none',
    'publishedAt': '2014-09-11T18:37:28.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/QrIrbeoDkT0/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/QrIrbeoDkT0/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/QrIrbeoDkT0/mqdefault.jpg',
      'width': 320}},
    'title': 'Ellen Meets the ‘Apparently’ Kid, Part 1'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/Ag0Zp0Gg9tiWeBYr4k1p5W7EnLI"',
   'id': {'kind': 'youtube#video', 'videoId': 'U8Gv83xiFP8'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'Emma Stone, Jamie Foxx and Andrew Garfield all participated in a revealing round of the saucy question and answer game.',
    'liveBroadcastContent': 'none',
    'publishedAt': '2014-04-04T04:55:12.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/U8Gv83xiFP8/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/U8Gv83xiFP8/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/U8Gv83xiFP8/mqdefault.jpg',
      'width': 320}},
    'title': "'The Amazing Spider-Man 2' Cast Plays Never Have I Ever"}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/EeSV1P1pL1VAVWYm27ev8YIWcTk"',
   'id': {'kind': 'youtube#video', 'videoId': 'QyJ8rulYHpU'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "Ellen had a star turn in Nicki's viral video. What did Nicki think? Find out!",
    'liveBroadcastContent': 'none',
    'publishedAt': '2014-09-10T05:38:21.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/QyJ8rulYHpU/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/QyJ8rulYHpU/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/QyJ8rulYHpU/mqdefault.jpg',
      'width': 320}},
    'title': 'Nicki Minaj Reacts to Ellen’s ‘Anaconda’'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/lklEBOJbEqTsZhzFeWQDvaEaovo"',
   'id': {'kind': 'youtube#video', 'videoId': '7nGz7xgGJzc'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "After Ellen saw Brielle's video on ellentube, she invited her to showcase her science smarts on the show!",
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-11-23T14:00:01.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/7nGz7xgGJzc/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/7nGz7xgGJzc/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/7nGz7xgGJzc/mqdefault.jpg',
      'width': 320}},
    'title': 'Adorable 3-Year-Old Periodic Table Expert Brielle'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/lFfDdAriFOK6-7T-GqAaQrfZrL0"',
   'id': {'kind': 'youtube#video', 'videoId': '2DYfLJrp1TQ'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'Ellen loves scaring her executive producer, Andy Lassner, and "Modern Family" star Eric Stonestreet. So, of course she couldn't wait to send them both through ...',
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-10-29T13:00:00.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/2DYfLJrp1TQ/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/2DYfLJrp1TQ/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/2DYfLJrp1TQ/mqdefault.jpg',
      'width': 320}},
    'title': 'Andy Goes to a Haunted House with Eric Stonestreet'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/UPK4p6u7VdWPSkW1Cnz5QKCxfXk"',
   'id': {'kind': 'youtube#video', 'videoId': 'fNJI2A0v8yI'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'Leonardo DiCaprio is quite the daredevil, and he told Ellen about a few of his close calls!',
    'liveBroadcastContent': 'none',
    'publishedAt': '2016-01-08T14:00:01.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/fNJI2A0v8yI/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/fNJI2A0v8yI/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/fNJI2A0v8yI/mqdefault.jpg',
      'width': 320}},
    'title': "Leo's Bad Luck"}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/cdwuhXK78q9_SFeRcIdz2ZKxvy4"',
   'id': {'kind': 'youtube#video', 'videoId': '3RLTanW1DGo'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'Their visit to the haunted house was so funny, Ellen had to send them again! This time Andy and Amy visited the Queen Mary Dark Harbor, and the results ...',
    'liveBroadcastContent': 'none',
    'publishedAt': '2013-10-31T13:00:03.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/3RLTanW1DGo/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/3RLTanW1DGo/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/3RLTanW1DGo/mqdefault.jpg',
      'width': 320}},
    'title': "Andy and Amy's Haunted Ship Adventure"}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/N1j-mxkND6apYO8kdjitbd3mOns"',
   'id': {'kind': 'youtube#video', 'videoId': 'zcAQCTZ3TuQ'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "Ellen held nothing back when when Mila Kunis was here, and asked her about what's going on between her and Ashton Kutcher. See how she responded!",
    'liveBroadcastContent': 'none',
    'publishedAt': '2013-02-13T17:14:14.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/zcAQCTZ3TuQ/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/zcAQCTZ3TuQ/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/zcAQCTZ3TuQ/mqdefault.jpg',
      'width': 320}},
    'title': 'Mila Kunis Blushes over Ashton'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/b7ZtfnV8spHzXmzgj_c0lN2dwJ0"',
   'id': {'kind': 'youtube#video', 'videoId': 'pP-PF4nKb0I'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'It was a legendary meeting on The Ellen Show, Sophia Grace & Rosie and Russell Brand met for the very first time to discuss their hometown of Essex, England ...',
    'liveBroadcastContent': 'none',
    'publishedAt': '2012-05-17T02:57:04.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/pP-PF4nKb0I/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/pP-PF4nKb0I/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/pP-PF4nKb0I/mqdefault.jpg',
      'width': 320}},
    'title': 'Sophia Grace & Rosie Meet Russell Brand'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/U3UJ8UFIu4nRHqfysq91oHzinuw"',
   'id': {'kind': 'youtube#video', 'videoId': 'mUr5KxtKZQk'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'After chatting with Sofia Vergara, Ellen sent her into a store on the WB lot for some hidden camera fun!',
    'liveBroadcastContent': 'none',
    'publishedAt': '2010-11-03T19:13:20.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/mUr5KxtKZQk/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/mUr5KxtKZQk/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/mUr5KxtKZQk/mqdefault.jpg',
      'width': 320}},
    'title': 'Sofia Vergara Plays a Hidden Camera Prank'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/B4kmiOv8FEBHoH3MRiahPrDEGxc"',
   'id': {'kind': 'youtube#video', 'videoId': '5SZ_--mt4bk'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': 'They were staked out in the bathroom for this hilarious round of scares!',
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-02-06T14:00:01.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/5SZ_--mt4bk/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/5SZ_--mt4bk/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/5SZ_--mt4bk/mqdefault.jpg',
      'width': 320}},
    'title': 'Justin Bieber and Ellen Scare Audience Members'}},

谢谢。

给定您引用的Jupyter笔记本,以下是如何从检索到的数据中获得videoId的方法。这回答你的问题了吗?

我不完全确定Youtube搜索API是如何工作的,但如果这不是一个完整的答案,我可能有时间去探索它。

example = {
    'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/EK5D70JgnA5Bec8tRSnEFfhIsv0"',
    'items': [
        {
            'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/hsQmFEqp1R_glFpcQnpnOLbbxCg"',
            'id': {
                'kind': 'youtube#video', 'videoId': 'd8kCTPPwfpM'},
            'kind': 'youtube#searchResult',
            'snippet': {
                'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
                'channelTitle': 'TheEllenShow',
            'description': "This incredible duo teamed up to perform an original song for Ellen! They may not have had a lot of rehearsal, but it's clear that this is one musical combo it ...",
            'liveBroadcastContent': 'none',
            'publishedAt': '2012-02-21T14:00:00.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/d8kCTPPwfpM/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/d8kCTPPwfpM/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/d8kCTPPwfpM/mqdefault.jpg',
      'width': 320}},
    'title': 'Taylor Swift and Zac Efron Sing a Duet!'}},
  {'etag': '"I_8xdZu766_FSaexEaDXTIfEWc0/LeKypRrnWWD6mRhK1wATZB5UQGo"',
   'id': {'kind': 'youtube#video', 'videoId': '-l2KPjQ2lJA'},
   'kind': 'youtube#searchResult',
   'snippet': {'channelId': 'UCp0hYYBW6IMayGgR-WeoCvQ',
    'channelTitle': 'TheEllenShow',
    'description': "Harry, Liam, Louis and Niall played a round of Ellen's revealing game. How well do you know the guys of One Direction?",
    'liveBroadcastContent': 'none',
    'publishedAt': '2015-11-18T14:00:00.000Z',
    'thumbnails': {'default': {'height': 90,
      'url': 'https://i.ytimg.com/vi/-l2KPjQ2lJA/default.jpg',
      'width': 120},
     'high': {'height': 360,
      'url': 'https://i.ytimg.com/vi/-l2KPjQ2lJA/hqdefault.jpg',
      'width': 480},
     'medium': {'height': 180,
      'url': 'https://i.ytimg.com/vi/-l2KPjQ2lJA/mqdefault.jpg',
      'width': 320}},
    'title': 'Never Have I Ever with One Direction'}},]}
import pprint
for video in example["items"]:
    pprint.pprint(video["id"]["videoId"])
# Prints 'd8kCTPPwfpM'
# '-l2KPjQ2lJA'

相关内容

  • 没有找到相关文章

最新更新