使用Python从youtube API v3中获取PlaylistItems数据

时间:2019-06-14 05:25:24

标签: python python-3.x

尝试从我的YouTube频道获取数据时,我得到了

  

“ IndexError:列表索引超出范围”

下面是代码:

def get_channel_videos(channel_id):

    res = youtube.channels().list(id=channel_id, 
                                  part='contentDetails').execute()
    playlist_id = res['items'][0]['contentDetails']['relatedPlaylists']['uploads']

    videos = []
    next_page_token = None

    while 1:
        res = youtube.playlistItems().list(playlistId=playlist_id, 
                                           part='snippet', 
                                           maxResults=50,
                                           pageToken=next_page_token).execute()
        videos += res['items']
        next_page_token = res.get('nextPageToken')

        if next_page_token is None:
            break

    return videos

videos = get_channel_videos('xxxxxxxxxxxxxxxxxxxxxxx')

0 个答案:

没有答案