You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PlaylistItems接口未返回全部视频名,如何获取历史所有视频名称?

YouTube视频名称缺失问题解答

一、PlaylistItems接口无法返回全部视频名称的原因

  • 你使用的UU{频道ID}是频道的自动“上传”播放列表,这个列表仅包含频道设为公开状态的视频,私有、未列出的视频不会被包含在内。而YouTube Reporting API会返回频道所有状态的视频数据(包括私有、未列出),因此两者关联时必然出现大量名称缺失。
  • 额外情况:如果视频被删除、因违规被平台移除,或手动从自动播放列表中移除(虽极少发生),也会导致PlaylistItems接口无法获取对应视频信息。

二、获取历史所有视频名称的最优HTTP请求

直接使用YouTube Data API的videos.list接口,通过你从Reporting API拿到的VideoID批量查询,这是最准确的方式:

GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=VIDEO_ID_1,VIDEO_ID_2,VIDEO_ID_3&maxResults=50
  • 参数说明:
    • part=snippet:指定返回包含视频标题、描述等核心信息的片段
    • id:传入逗号分隔的VideoID列表,单次最多可传50个
    • 若需访问私有/未列出视频,需携带OAuth2.0授权凭证(而非仅API密钥)

代码优化方向

替换原依赖PlaylistItems的逻辑,改为直接批量查询视频信息:

import json
import glob
import os
import csv
import re

def get_video_details(video_ids, svc):
    video_map = {}
    # 分批处理,每次最多50个ID
    for i in range(0, len(video_ids), 50):
        batch_ids = video_ids[i:i+50]
        rsp = svc.videos().list(part='snippet', id=','.join(batch_ids)).execute()
        for item in rsp['items']:
            video_map[item['id']] = {
                'title': item['snippet']['title'],
                'description': item['snippet']['description'],
                'publishedAt': item['snippet']['publishedAt']
            }
    return video_map

def make_complete_videos_csv():
    htag = re.compile(r"\s#\S+")
    # 第一步:从Reporting API的结果中提取所有VideoID
    all_video_ids = []
    # 此处补充读取Reporting API视频数据、提取VideoID的逻辑
    # ...
    
    # 第二步:初始化YouTube Data API客户端
    jsn = json.load(open(TOKEN_PATH + token_file))
    svc = get_youtube_data(jsn)
    video_details = get_video_details(all_video_ids, svc)
    
    # 第三步:生成完整CSV
    with open(f'YoutubeAnalytics/videos/videos_full.csv', 'w', encoding='utf-8', newline='') as csvf:
        wrt = csv.writer(csvf)
        for video_id in all_video_ids:
            details = video_details.get(video_id, {})
            descr = details.get('description', '')
            tags = ','.join([t[1:] for t in htag.findall(descr)])
            wrt.writerow((
                video_id,
                details.get('title', '视频名称缺失'),
                details.get('publishedAt', ''),
                descr,
                tags
            ))

原代码核心问题:依赖频道自动上传播放列表的逻辑,天然过滤了非公开视频,无法覆盖Reporting API返回的全部视频范围。

内容的提问来源于stack exchange,提问作者Luciebix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 17:40:38