Search code examples
pythonpython-3.x

pytube library could not find match for ^\w+\W


i recently tried to download a youtube video using pytube library as below :

from pytube import YouTube

def download_video(url, resolution):
    try:
        yt = YouTube(url)
        streams = yt.streams.filter(res=f'{resolution}p', progressive=True).first()
        if streams:
            print(f"Downloading {yt.title} in {resolution}p resolution...")
            streams.download()
            print("Download completed successfully!")
        else:
            print(f"No {resolution}p resolution available for this video.")
    except Exception as e:
        print("An error occurred:", str(e))

if __name__ == "__main__":
    url = input("Enter the YouTube video link: ")
    resolution = input("Enter the desired resolution (e.g., 144, 240, 360, 480, 720, 1080): ")
    download_video(url, resolution)

but when i specify the resolution it brings me back this error :

An error occurred: __init__: could not find match for ^\w+\W

i search the internet for any solution and find this :

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")

but unfortunately it doesn't work for me either. it is very strange because my code worked fine yesterday but now it doesn't work anymore. i am using google colab and my location is germany.


Solution

  • Try using this instead:

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

    It worked on my end.
    Found the answer here