9

So my issue is I run this simple code to attempt to make a pytube stream object...

from pytube import YouTube

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

And end up with the error in the title.

Full error:

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

Extra data:

python version: 3.8.10 pytube version: 11.0.2

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

63

As juanchosaravia suggested on https://github.com/pytube/pytube/issues/1199, in order to solve the problem, you should go in the cipher.py file and replace the line 30, which is:

var_regex = re.compile(r"^\w+\W")

With that line:

var_regex = re.compile(r"^\$*\w+\W")

After that, it worked again.

RidiX
  • 819
  • 6
  • 9
2

To go cipher.py in the pytube site packages

Change line 30
var_regex = re.compile(r"^\w+\W")

to

var_regex = re.compile(r"^$*\w+\W")

"^" means search beginning of the line
".*" means zero or more of any character
"$" means to end of line

-1

In order to solve the problem of " init: could not find match for ^\w+\W ", you have to edit a line in cipher.py file which is located in

C:\Users\#####\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytube\cipher.py

replace the line 30, which is:

var_regex = re.compile(r"^\w+\W")

with this line :

var_regex = re.compile(r"^$\w+\W")

It will work again.

  • 1
    what value does this answer add to the question? as far as I can tell, this is the exact same information as is contained in the two other answers, besides the path, which is only valid in very specific cases? – Hoodlum Aug 16 '23 at 20:38
  • @Hoodlum Question was specific to PyTube and so do the path mentioned and If it is saving even a second then I think it contains value in it. Everybody can have different perceptions of the things. – Manvendra Singh Aug 24 '23 at 18:42