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

pytube报错'NoneType' object has no attribute 'span'如何解决

pytube 获取视频流时触发 AttributeError: 'NoneType' object has no attribute 'span' 修复方案

完整报错栈信息:

Traceback (most recent call last):   File "<stdin>", line 1, in <module>
File "C:\Users\2021\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 292, in streams
    return StreamQuery(self.fmt_streams)
File "C:\Users\2021\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 177, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "C:\Users\2021\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
File "C:\Users\2021\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\cipher.py", line 44, in __init__
    self.throttling_array = get_throttling_function_array(js)
File "C:\Users\2021\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\cipher.py", line 323, in get_throttling_function_array
    str_array = throttling_array_split(array_raw)
File "C:\Users\2021\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\parser.py", line 158, in throttling_array_split
    match_start, match_end = match.span() AttributeError: 'NoneType' object has no attribute 'span'

问题原因

该报错由YouTube调整播放器JS的节流函数声明格式导致,pytube内置的正则匹配规则无法适配新版JS结构,因此匹配结果为空,调用span方法时报错。该问题和本地pytube安装状态无关,重装不同版本无法解决。

修复步骤

  • 打开报错路径下的pytube/parser.py文件,参考路径:C:\Users\2021\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\parser.py
  • 找到throttling_array_split函数中原有的正则匹配代码,替换为如下兼容不同变量声明方式的匹配逻辑:
# 依次匹配const、let、var三种变量声明方式
match = re.search(r"(?<=const )\w+\s*=\s*(\[.+?\]);", array_raw, re.DOTALL)
if not match:
    match = re.search(r"(?<=let )\w+\s*=\s*(\[.+?\]);", array_raw, re.DOTALL)
if not match:
    match = re.search(r"(?<=var )\w+\s*=\s*(\[.+?\]);", array_raw, re.DOTALL)
  • 保存修改后的文件,重新运行原有pytube代码即可恢复正常。

后续说明

后续pytube官方发布适配新版YouTube规则的版本后,直接升级官方正式版即可覆盖手动修改的内容,无需重复手动调整。

内容的提问来源于stack exchange,提问作者Never stop aiming high

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 02:06:04