使用pytube下载YouTube视频时出现NoneType无span属性错误
解决pytube的AttributeError: 'NoneType' object has no attribute 'span'问题
这个错误是因为YouTube更新了视频流的加密逻辑,官方pytube版本的代码未适配新结构,导致get_throttling_plan函数匹配不到目标代码片段,返回None后调用span()引发异常。
修复步骤:
- 卸载当前版本的pytube:
pip uninstall pytube -y
- 安装pytube官方仓库的最新修复版本:
pip install git+https://github.com/pytube/pytube.git
可选优化(针对受限视频):
如果下载部分需要验证的视频仍有问题,可在初始化YouTube对象时添加OAuth参数,让程序自动完成浏览器验证:
yt = YouTube(user_input, on_progress_callback=progress_function, on_complete_callback=completed_function, use_oauth=True, allow_oauth_cache=True)
原问题代码与报错信息:
代码:
def progress_function(stream, chunk, bytes_remaining): print("Downloading") def completed_function(stream, file_path): print("download completed") messagebox.showinfo(title="Download has completed", message="The video has successfully downloaded") def download(): user_input = str(link.get()) yt = YouTube(user_input, on_progress_callback=progress_function, on_complete_callback=completed_function) print(yt.title) print(yt.thumbnail_url) video = yt.streams.get_highest_resolution() video.download()
报错信息:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\---\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948,in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "C:\Users\---\PycharmProjects\youtube downloader\main.py", line 30, in download video = yt.streams.get_highest_resolution() ^^^^^^^^^^ File "C:\Users\---\PycharmProjects\youtube downloader\venv\day\QuizGame\Lib\site-packages\pytube\__main__.py", line 296, in streams return StreamQuery(self.fmt_streams) ^^^^^^^^^^^^^^^^ File "C:\Users\---\PycharmProjects\youtube downloader\venv\day\QuizGame\Lib\site-packages\pytube\__main__.py", line 181, in fmt_streams extract.apply_signature(stream_manifest, self.vid_info, self.js) File "C:\Users\---\PycharmProjects\youtube downloader\venv\day\QuizGame\Lib\site-packages\pytube\extract.py", line 409, in apply_signature cipher = Cipher(js=js) ^^^^^^^^^^^^^ File "C:\Users\---\PycharmProjects\youtube downloader\venv\day\QuizGame\Lib\site-packages\pytube\cipher.py", line 29, in __init__ self.throttling_plan = get_throttling_plan(js) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\---\PycharmProjects\youtube downloader\venv\day\QuizGame\Lib\site-packages\pytube\cipher.py", line 404, in get_throttling_plan transform_plan_raw = find_object_from_startpoint(raw_code, match.span()[1] - 1) ^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'span'
内容的提问来源于stack exchange,提问作者Noran nm
相关产品推荐
相关产品推荐

