Pytube报错AttributeError: 'NoneType'对象无span属性求助
问题:Pytube 抛出 AttributeError: 'NoneType' object has no attribute 'span'
昨日程序运行正常,今日执行时出现错误。此前仅为适配新YouTube链接手动更新了pytube的cipher.py文件。
程序代码
from pytube import YouTube url = input("Give link: ") video = YouTube(url) video_audio = video.streams.get_audio_only() video_audio.download(filename_prefix="Audio ")
报错信息
Traceback (most recent call last): File "C:\Users\Admin\Desktop\Python\YTDowlander.py", line 8, in <module> video_audio = video.streams.get_audio_only() File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\__main__.py", line 296, in streams return StreamQuery(self.fmt_streams) File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\__main__.py", line 181, in fmt_streams extract.apply_signature(stream_manifest, self.vid_info, self.js) File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\extract.py", line 414, in apply_signature cipher = Cipher(js=js) File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\cipher.py", line 44, in __init__ self.throttling_plan = get_throttling_plan(js) File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\cipher.py", line 412, in get_throttling_plan transform_plan_raw = find_object_from_startpoint(raw_code, match.span()[1] - 1) AttributeError: 'NoneType' object has no attribute 'span'
解决方案
- 优先安装pytube最新开发版,官方会实时跟进YouTube的JS结构变化,无需手动修改文件:
pip install git+https://github.com/pytube/pytube.git - 若坚持使用旧版本,检查
cipher.py中get_throttling_plan函数的正则表达式,确认其能匹配当前YouTube页面的JS代码——报错原因是正则匹配失败,match变量为None,无法调用span()方法。 - 直接替换
cipher.py中get_throttling_plan函数为官方仓库的最新实现,确保逻辑与YouTube当前的JS结构兼容。
内容的提问来源于stack exchange,提问作者Artur
相关产品推荐
相关产品推荐

