从youtube api(python)中的播放列表中获取标题

时间:2017-08-25 12:14:05

标签: python youtube-api

问题是获取视频列表中每个项目的标题并进行打印,但打印功能在功能上不起作用,并且看不到变量的功能。

多一点编辑的文字

from __future__ import unicode_literals
import requests
import json

url = "https://www.youtube.com/chanel/UC1ctnuRGUUXe04EgbsYXwPQ"
PLAYLIST_ID = 'PLg5SS_4L6LYueOJFm-IdWSuLiYwcASkH-'
APIKEY = '***********'


def get_items():

        YOUTUBE_URI = 'https://www.googleapis.com/youtube/v3/playlistItems'
        FORMAT = {'key': APIKEY, 'part': 'snippet', 'playlistId': PLAYLIST_ID, 'maxResults': 50}
        FORMAT_YOUTUBE_URI = (YOUTUBE_URI, FORMAT)
        content = requests.get(FORMAT_YOUTUBE_URI).json()

        video_list = []
        keys = 'id', 'title', 'description'

        for item in content('items'):
            id = item.get('id').get('videoId')
            title = item.get('snippet').get('title')
            description = item.get('snippet').get('description')
            values = id, title, description

            if id:
                video_item = dict(zip(keys, values))
                video_list.append(video_item)
                print('\n'.join(video_list))
                return video_list

1 个答案:

答案 0 :(得分:1)

永远不会使用

content变量,我可以看到数据所在的位置。除了使用vide_list但从未填充有用数据之外,只使用空列表[]初始化

我强烈建议你使用某种编辑器来帮助你轻松发现这些事情。

编辑后:

content = requests.get(FORMAT_YOUTUBE_URI).json()

是有问题的,而不是出现的逻辑,因此您应该集中精力制作正确的Youtube API请求,然后了解如何处理结果。