使用Pytube筛选视频流时遭遇AttributeError问题求助
使用pytube下载YouTube视频时触发AttributeError错误
我尝试用pytube库下载YouTube视频,筛选视频流时出现了以下错误。
核心代码
from pytube import YouTube from moviepy.editor import VideoFileClip, AudioFileClip import os link = input("Enter link here: ") filename = input("Enter desired filename here: ") filename = filename.replace('"', '').replace('&', 'and') url = YouTube(link) print("downloading....") video = url.streams.filter(res="2160p", mime_type="video/webm").first()
错误信息
File "C:\Users\user\Desktop\YouTube.py", line 15, in <module> video = url.streams.filter(res="2160p", mime_type="video/webm").first() File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pytube\__main__.py", line 296, in streams return StreamQuery(self.fmt_streams) File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pytube\__main__.py", line 181, in fmt_streams extract.apply_signature(stream_manifest, self.vid_info, self.js) File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pytube\extract.py", line 409, in apply_signature cipher = Cipher(js=js) File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pytube\cipher.py", line 43, in __init__ self.throttling_plan = get_throttling_plan(js) File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pytube\cipher.py", line 411, 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代码更新,导致无法匹配到节流计划相关代码造成的,可尝试以下方案:
升级pytube到最新版本
终端执行命令:pip install --upgrade pytube官方会持续修复YouTube的API变动问题,升级通常能解决这类解析错误。
安装pytube的master分支版本
如果官方稳定版未修复,可尝试社区维护的最新分支:pip install git+https://github.com/pytube/pytube.git@master临时修改cipher.py文件
找到错误信息中显示的pytube安装目录下的cipher.py,替换get_throttling_plan函数为以下代码(适配当前YouTube的JS结构):def get_throttling_plan(js: str) -> Dict: # 匹配最新的节流计划模式 pattern = r'throttlingPlan\s*=\s*({.+?});' match = re.search(pattern, js, re.DOTALL) if not match: pattern = r'var\s+throttling\s*=\s*({.+?});' match = re.search(pattern, js, re.DOTALL) if not match: raise RegexMatchError( caller="get_throttling_plan", pattern=pattern ) return json.loads(match.group(1))注意:该修改会在下次升级pytube时被覆盖,仅作为临时方案。
切换到yt-dlp库
如果pytube频繁出问题,可改用更新更稳定的yt-dlp:pip install yt-dlp对应下载代码示例:
import yt_dlp link = input("Enter link here: ") filename = input("Enter desired filename here: ") filename = filename.replace('"', '').replace('&', 'and') ydl_opts = { 'format': 'bestvideo[ext=webm][height=2160]+bestaudio[ext=webm]/best[ext=webm]', 'outtmpl': f'{filename}.%(ext)s', } with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([link])
内容的提问来源于stack exchange,提问作者Kasmori
相关产品推荐
相关产品推荐

