pytube.exceptions.RegexMatchError:__init__:找不到 ^\w+\W 的匹配项

asl*_*hdj 9 python pytube

所以我的问题是我运行这个简单的代码来尝试创建一个 pytube 流对象......

from pytube import YouTube

yt = YouTube('https://www.youtube.com/watch?v=aQNrG7ag2G4')
stream = yt.streams.filter(file_extension='mp4')
Run Code Online (Sandbox Code Playgroud)

并最终出现标题中的错误。

完整错误:

Traceback (most recent call last):
  File ".\test.py", line 4, in <module>
    stream = yt.streams.filter(file_extension='mp4')
  File "C:\Users\logan\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 292, in streams       
    return StreamQuery(self.fmt_streams)
  File "C:\Users\logan\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 184, in fmt_streams   
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "C:\Users\logan\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "C:\Users\logan\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 33, in __init__
    raise RegexMatchError(
pytube.exceptions.RegexMatchError: __init__: could not find match for ^\w+\W
Run Code Online (Sandbox Code Playgroud)

额外数据:

python版本:3.8.10 pytube版本:11.0.2

小智 63

正如 juanchosaravia 在https://github.com/pytube/pytube/issues/1199上建议的那样,为了解决该问题,您应该进入 cipher.py 文件并替换第 30 行,即:

var_regex = re.compile(r"^\w+\W")
Run Code Online (Sandbox Code Playgroud)

用那行:

var_regex = re.compile(r"^\$*\w+\W")
Run Code Online (Sandbox Code Playgroud)

之后,它又起作用了。

  • 截至 2023 年 8 月 12 日,仍在 pytube 版本 15.0.0 上作为解决方案运行 (2认同)