【问题标题】:How to put youtube captions in a list?如何将 youtube 字幕放入列表中?
【发布时间】:2021-12-26 17:51:25
【问题描述】:

“文本”字符串包含 youtube 视频的字幕。如何将这些字幕放在每个视频的单独列表中。

videos = get_channel_videos(channel_id)
video_ids = []  # list of all video_id of channel
 
for video in videos:
    video_ids.append(video['snippet']['resourceId']['videoId'])
 
for video_id in video_ids:
    try:
        responses = YouTubeTranscriptApi.get_transcript(
            video_id, languages=['en'])
        print('\n'+"Video: "+"https://www.youtube.com/watch?v="+str(video_id)+'\n'+'\n'+"Captions:")
        for response in responses:
            text = response['text']
            print(text)
    except Exception as e:
       print(e)

【问题讨论】:

    标签: python youtube-api


    【解决方案1】:

    您可能需要一个列表字典,而不仅仅是很多列表:

    videos = get_channel_videos(channel_id)
    video_ids = []  # list of all video_id of channel
     
    for video in videos:
        video_ids.append(video['snippet']['resourceId']['videoId'])
    
    # make your dictionary here
    captions = {}
     
    for video_id in video_ids:
        if video_id not in captions:
            # initialize empty list for each id
            captions[video_id] = [] 
    
        try:
            responses = YouTubeTranscriptApi.get_transcript(
                video_id, languages=['en'])
            print('\n'+"Video: "+"https://www.youtube.com/watch?v="+str(video_id)+'\n'+'\n'+"Captions:")
            for response in responses:
                text = response['text']
    
                # append to that list here
                captions[video_id].append(text)
    
                print(text)
        except Exception as e:
           print(e)
    

    对于多个字段

    你需要一本字典:

    for video_id in video_ids:
        if video_id not in captions:
            # initialize empty list for each id
            captions[video_id] = {'Transcript': [], 'Title': None}
        
        try:
            responses = YouTubeTranscriptApi.get_transcript(
                video_id, languages=['en'])
            print('\n'+"Video: "+"https://www.youtube.com/watch?v="+str(video_id)+'\n'+'\n'+"Captions:")
            for response in responses:
                text = response['text']
    
                # append to that list here
                captions[video_id]['Transcript'].append(text)
    
                print(text)
    
            captions[video_id]['title'] = response['title']
        except Exception as e:
           print(e)
    

    【讨论】:

    • 您知道如何将 youtube 视频的标题添加到字典中吗?所以输出的例子是 {'Key': ['transcript'],[ 'title']}
    • @SlimDowning 查看最新编辑
    • 执行代码时,值'None'在每一行的标题字典中。同样在 sn-p 'captions[video_id]['title'] = response['title']' Pycharm 给出一个错误,即名称'response'可以是未定义的
    • 正确,如果responses 为空,则不会定义response
    • 我希望在响应中填写当前显示为“无”的标题
    猜你喜欢
    • 2012-10-22
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2018-04-09
    相关资源
    最近更新 更多