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

使用Pytube获取YouTube视频标题时出现KeyError错误求助

解决Pytube获取YouTube视频标题时的KeyError问题

问题重现

运行以下代码获取YouTube视频标题时,触发KeyError: 'videoDetails'及PytubeError:

from pytube import YouTube

link = input("Enter the video link here ->")

y_tube = YouTube(link)

print(y_tube.title)

执行后报错信息:

Enter the video link here ->https://youtu.be/rkR4sFODnIM?si=FqDP5s3TEDCMStqz
Traceback (most recent call last):
  File "C:\Users\kumar_0nqo0ht\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\__main__.py", line 341, in title
    self._title = self.vid_info['videoDetails']['title']
                  ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: 'videoDetails'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Projects\videodownloader\main.py", line 8, in <module>
    print(f'y_tube.Title->{y_tube.title}')
                           ^^^^^^^^^^^^
  File "C:\Users\kumar_0nqo0ht\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\__main__.py", line 346, in title
    raise exceptions.PytubeError(
pytube.exceptions.PytubeError: Exception while accessing title of https://youtube.com/watch?v=rkR4sFODnIM. Please file a bug report at https://github.com/pytube/pytube

解决方案

  • 升级Pytube到最新版本:YouTube会频繁调整接口规则,旧版本Pytube无法适配,执行命令:
    pip install --upgrade pytube
    
  • 清理链接冗余参数:链接中携带的额外参数(如示例中的?si=FqDP5s3TEDCMStqz)可能干扰解析,简化为纯视频地址即可,比如将原链接改为https://youtu.be/rkR4sFODnIM或https://www.youtube.com/watch?v=rkR4sFODnIM。
  • 禁用缓存机制:Pytube的缓存可能留存旧数据导致解析失败,初始化对象时禁用缓存:
    y_tube = YouTube(link, use_oauth=False, allow_oauth_cache=False)
    
  • 临时替代方案:若上述方法无效,可手动提取视频ID后尝试调用Pytube内部接口,或更换yt-dlp等替代库完成视频信息获取。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 09:56:04