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

Posted

技术标签:

【中文标题】Python 将变量输出拆分为多个变量【英文标题】:Python Split Variable Output to Multiple Variables 【发布时间】:2017-02-25 17:29:31 【问题描述】:

首先,我在这里为我的无知道歉,因为我对 Python 很陌生,我可能会说一些真正没有意义的话。

我正在使用youtube.search.list API 创建变量search_response。此变量以 JSON 格式输出所有搜索到的视频的数据(我相信无论如何都是 JSON)。

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

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

使用videos.list API,我已经能够提取这些变量(查看、喜欢、不喜欢),但它似乎也被限制为 50 个结果并且不提供分页选项。

Here is a link 到 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()

理想输出:此输出对应于前 50 个 etag 部分(由于本网站的字符限制,此处仅显示 25 个左右,但应该是 50 个部分) em> search_response 变量并且可以被调用 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 Have\xa0I\xa0Ever',
  '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 Performs\xa0'All\xa0I\xa0Ask'",
  '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',

谢谢。

【问题讨论】:

你已经很好地解决了你的问题(谢谢!),但如果你能包含一个(可能简化的)你得到什么和你想要什么的例子,那就更清楚了. 另外,当 ASGM 说示例时,他们说的是您使用的代码不起作用。 当您谈论 50 个项目时,您是在谈论要搜索的 50 个 etags 列表吗? @AlexLordThorsen 感谢您的回复,但我对 etag 一词不是 100% 清楚,这是网站内的唯一标识符吗?我想我更想寻找一个包含 50 个videoId 的列表,我可以用它来搜索详细的统计数据。我添加了一段用于提取详细统计信息的代码。 【参考方案1】:

鉴于您在此处引用的 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'

【讨论】:

这确实可以打印所有 videoIds,但我需要一种方法将它们分成 50 个变量,以便我可以为我的 添加详细统计信息 进行搜索。我想我可能需要保持原始输出完好无损(如您的示例变量所示),但将其分成 50 个段,其中 example 有 2 个段。这有意义吗?感谢您在这方面的帮助! 这对我来说不太合理。您能否向我展示您正在寻找的示例输出? 我在标题理想输出下发布了我正在寻找的示例。

以上是关于Python 将变量输出拆分为多个变量的主要内容,如果未能解决你的问题,请参考以下文章

python序列的拆分

vbscript 根据列变量将数据拆分为多个工作表 - 从在线资源编辑

使用正则表达式将字符串拆分为多个变量 SAS

python的print输出多个变量

pandas将初始dataframe基于分组变量拆分为多个新的dataframe使用groupby函数tuple函数dict函数(splitting dataframe multiple)

无需指定环境变量/系统属性即可将 Spring Boot 配置拆分为多个属性文件的任何方法