使用Pytube下载YouTube视频遇RegexMatchError问题求助
解决Pytube 15.0.0的RegexMatchError问题
问题详情
环境:Python 3.9.9、Pytube 15.0.0
使用以下代码下载YouTube视频时出现错误:
from pytube import YouTube video = YouTube('https://www.youtube.com/watch?v=AWXvSBHB210') video.streams.get_highest_resolution().download()
错误信息(翻译后)
Traceback (most recent call last): 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\__main__.py", line 181, in fmt_streams extract.apply_signature(stream_manifest, self.vid_info, self.js) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\extract.py", line 409, in apply_signature cipher = Cipher(js=js) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\cipher.py", line 43, in __init__ self.throttling_plan = get_throttling_plan(js) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\cipher.py", line 405, in get_throttling_plan raw_code = get_throttling_function_code(js) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\cipher.py", line 311, in get_throttling_function_code name = re.escape(get_throttling_function_name(js)) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\cipher.py", line 296, in get_throttling_function_name raise RegexMatchError( pytube.exceptions.RegexMatchError: get_throttling_function_name: 无法匹配到对应的函数名 在处理上述异常期间,又触发了新的异常: Traceback (most recent call last): 文件 "c:\Users\Tobias\Desktop\Exal\test01.py", line 4, in <module> video.streams.get_highest_resolution().download() 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\__main__.py", line 296, in streams return StreamQuery(self.fmt_streams) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\__main__.py", line 188, in fmt_streams extract.apply_signature(stream_manifest, self.vid_info, self.js) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\extract.py", line 409, in apply_signature cipher = Cipher(js=js) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\cipher.py", line 43, in __init__ self.throttling_plan = get_throttling_plan(js) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\cipher.py", line 405, in get_throttling_plan raw_code = get_throttling_function_code(js) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\cipher.py", line 311, in get_throttling_function_code name = re.escape(get_throttling_function_name(js)) 文件 "C:\Users\Tobias\AppData\Roaming\Python\Python39\site-packages\pytube\cipher.py", line 296, in get_throttling_function_name raise RegexMatchError( pytube.exceptions.RegexMatchError: get_throttling_function_name: 无法匹配到对应的函数名
解决方案
1. 升级Pytube到最新版本
YouTube会频繁更新页面逻辑,旧版本Pytube的正则规则无法适配。执行以下命令升级:
pip install --upgrade pytube
2. 手动修改cipher.py的正则匹配规则
如果升级后问题仍存在,找到Pytube安装目录下的cipher.py文件(路径示例:C:\Users\你的用户名\AppData\Roaming\Python\Python39\site-packages\pytube\cipher.py),替换get_throttling_function_name函数为:
def get_throttling_function_name(js: str) -> str: pattern = r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&\s*' \ r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)' match = re.search(pattern, js) if not match: pattern = r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)' match = re.search(pattern, js) if not match: raise RegexMatchError( caller="get_throttling_function_name", pattern=pattern ) return match.group(1)
3. 改用Pytube的维护fork版本
官方Pytube更新不及时时,可以使用社区维护的pytubefix:
pip install pytubefix
修改代码为:
from pytubefix import YouTube video = YouTube('https://www.youtube.com/watch?v=AWXvSBHB210') video.streams.get_highest_resolution().download()
内容的提问来源于stack exchange,提问作者Wolfie WS
相关产品推荐
相关产品推荐

