📌  相关文章
📜  如何使用Python从 YouTube 播放列表中提取图像信息?

📅  最后修改于: 2022-05-13 01:55:17.112000             🧑  作者: Mango

如何使用Python从 YouTube 播放列表中提取图像信息?

先决条件: YouTube API

Google 提供了大量 API 供开发人员选择。 Google 提供的每项服务都有一个关联的 API。作为其中之一,YouTube 数据 API 使用起来非常简单,可提供以下功能:

  • 搜索视频
  • 处理视频,例如检索有关视频的信息、插入视频、删除视频等。
  • 处理订阅,例如列出所有订阅、插入或删除订阅。
  • 检索有关评论的信息,例如对由 parentid 等标识的特定评论的回复。

在本文中,我们将学习如何使用Python从 YouTube 播放列表中提取单个视频的图像信息

我们将获取的图像信息:

  • 图片网址
  • 图像宽度和高度
  • 不同大小的图像

在 YouTube 视频中,有五种不同的缩略图:

  • 默认
  • 高的
  • 最大分辨率
  • 中等的
  • 标准

方法:

  • 我们将使用buildlistexecute方法,它会给出播放列表详细信息
  • 在列表方法中,在 part 属性中传递contentDetails ,在playlistId属性中传递PlaylistIDPlaylistURL
  • maxResultspageToken中传递 50 值最初传递
Python3
nextPageToken=None
  
# creating youtube resource object 
youtube = build('youtube','v3',developerKey='Enter API Key')
  
while True:
  
  # retrieve youtube video results 
  pl_request = youtube.playlistItems().list(
    part='snippet',
    playlistId=playlistId.get(),
    maxResults=50,
    pageToken=nextPageToken
  )
  pl_response = pl_request.execute()
  
  nextPageToken = pl_response.get('nextPageToken')
  
  if not nextPageToken:
    break


Python3
# import module
from googleapiclient.discovery import build
  
def playlist_video_links(playlistId):
  
    nextPageToken=None
      
    # creating youtube resource object 
    youtube = build('youtube','v3',developerKey='Enter API Key')
      
    while True:
  
        # retrieve youtube video results 
        pl_request = youtube.playlistItems().list(
            part='snippet',
            playlistId=playlistId,
            maxResults=50,
            pageToken=nextPageToken
            )
        pl_response = pl_request.execute()
  
        # Iterate through all response and fetch Image Information
        for item in pl_response['items']:
  
            thumbnails = item['snippet']['thumbnails']
  
            if 'default' in thumbnails:
                default = thumbnails['default']
                print(default)
  
            if 'high' in thumbnails:
                high = thumbnails['high']
                print(high)
  
            if 'maxres' in thumbnails:
                maxres = thumbnails['maxres']
                print(maxres)
  
            if 'medium' in thumbnails:
                medium = thumbnails['medium']
                print(medium)
  
            if 'standard' in thumbnails:
                standard = thumbnails['standard']
                print(standard)
            print("\n")
              
        nextPageToken = pl_response.get('nextPageToken')
  
        if not nextPageToken:
            break
  
playlist_video_links('PLsyeobzWxl7r2ukVgTqIQcl-1T0C2mzau')


  • 遍历所有响应并获取图像信息

下面是实现:

蟒蛇3

# import module
from googleapiclient.discovery import build
  
def playlist_video_links(playlistId):
  
    nextPageToken=None
      
    # creating youtube resource object 
    youtube = build('youtube','v3',developerKey='Enter API Key')
      
    while True:
  
        # retrieve youtube video results 
        pl_request = youtube.playlistItems().list(
            part='snippet',
            playlistId=playlistId,
            maxResults=50,
            pageToken=nextPageToken
            )
        pl_response = pl_request.execute()
  
        # Iterate through all response and fetch Image Information
        for item in pl_response['items']:
  
            thumbnails = item['snippet']['thumbnails']
  
            if 'default' in thumbnails:
                default = thumbnails['default']
                print(default)
  
            if 'high' in thumbnails:
                high = thumbnails['high']
                print(high)
  
            if 'maxres' in thumbnails:
                maxres = thumbnails['maxres']
                print(maxres)
  
            if 'medium' in thumbnails:
                medium = thumbnails['medium']
                print(medium)
  
            if 'standard' in thumbnails:
                standard = thumbnails['standard']
                print(standard)
            print("\n")
              
        nextPageToken = pl_response.get('nextPageToken')
  
        if not nextPageToken:
            break
  
playlist_video_links('PLsyeobzWxl7r2ukVgTqIQcl-1T0C2mzau')

输出: