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

Python旧代码运行报错:Tkinter回调出现KeyError: 'streamingData'

YouTube音频下载工具报错KeyError: 'streamingData'

问题详情

原本正常运行的Python YouTube音频下载工具,启动后能正常显示URL输入窗口,但输入视频链接点击下载按钮时,触发Tkinter回调错误,抛出KeyError: 'streamingData'。

错误栈

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 531, in _clicked
    self._command()
  File "C:\Users\PC\Desktop\youtube converter\main.py", line 17, in Download
    youtubeObject = youtubeObject.streams.get_audio_only()
  File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 296, in streams
    return StreamQuery(self.fmt_streams)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 176, in fmt_streams
    stream_manifest = extract.apply_descrambler(self.streaming_data)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 161, in streaming_data
    return self.vid_info['streamingData']
KeyError: 'streamingData'

原main.py代码

import customtkinter

from pytube import YouTube


customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")

root = customtkinter.CTk()

root.geometry("500x350")


def Download():
    youtubeObject = entry.get()
    youtubeObject = YouTube(youtubeObject)
    youtubeObject = youtubeObject.streams.get_audio_only()
    try:
        youtubeObject.download()
    except:
        print("Error!")
    print("Success!")


frame = customtkinter.CTkFrame(master=root)
frame.pack(pady = 20, padx = 60, fill="both", expand = True)


entry = customtkinter.CTkEntry(master=frame, placeholder_text= "URL")
entry.pack(pady=12, padx = 10)

button = customtkinter.CTkButton(master=frame, text="Download", command=Download)
button.pack(pady=12, padx = 10)

root.mainloop()

解决方案

这个错误是因为Pytube无法获取YouTube的streamingData,通常是YouTube页面结构更新、Pytube版本过旧或请求被拦截导致的,可尝试以下方法:

方法1:升级Pytube到最新版本

执行命令更新库:

pip install --upgrade pytube

方法2:安装Pytube官方修复分支

若升级后问题仍存在,安装官方仓库的最新修复版本:

pip install git+https://github.com/pytube/pytube.git@master

方法3:添加自定义请求头

YouTube可能拦截无特征请求,初始化YouTube对象时添加User-Agent:
修改Download函数如下:

def Download():
    youtube_url = entry.get()
    yt = YouTube(
        youtube_url,
        headers={
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
        }
    )
    audio_stream = yt.streams.get_audio_only()
    try:
        audio_stream.download()
        print("下载成功!")
    except Exception as e:
        print(f"下载失败:{str(e)}")

方法4:检查视频权限

确认输入的视频是公开可访问的,私有视频、年龄限制或地区限制视频会导致无法获取streamingData。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 14:15:20