Search code examples
pythonpytube

can't get Youtube video with pytube library


I coding a youtube video downloader if I using Youtube() to get video it's doesn't work

my code:

from pytube import YouTube
import os
yt = YouTube("https://www.youtube.com/watch?v=-7F40MVG0Wc")
print(yt.title)
stream = yt.streams.first()
stream.download()

in the third line I get a lot of errors

errors I get:

Traceback (most recent call last):
  File "d:\Projects\Youtube Video Downloader\index5.py", line 3, in <module>
    yt = YouTube("https://www.youtube.com/watch?v=-7F40MVG0Wc")

How can me fix this?


Solution

  • Please confirm that you are on the latest version of pytube by installing from the source.

    You can do this by running python -m pip install git+https://github.com/pytube/pytube Sometimes, the pypi library repository is not up to date, and your issue may have been fixed already!

    In C:\Python38\lib\site-packages\pytube\cipher.py

    Change this line:

    293: name = re.escape(get_throttling_function_name(js))

    to this:

    293: name = "iha"

    this Solution is mentioned in Github issues and it's working fine.

    Here is an example:

    from pytube import YouTube
    import os
    from pathlib import Path
    
    
    url = YouTube('https://www.youtube.com/watch?v=y-9MaAW_9dY')
    
    print("downloading....")
    
    video = url.streams.get_highest_resolution()
    
    path_to_download_folder = str(os.path.join(Path.home(), "Downloads"))
    
    video.download(path_to_download_folder)
    print("Downloaded! :)")