Search code examples
pythonpytube

"AttributeError: no attribute 'download " With PyTube


I have a problem with pytube when use YouTube

from pytube import YouTube
url = str(input("Youtube video url :"))
youtube = YouTube(url)
stream = youtube.streams()
video.download(0)

this error shows

AttributeError: 'YouTube' object has no attribute 'download'

and when I use playlist

from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PLRfY4Rc-GWzhdCvSPR7aTV0PJjjiSAGMs')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
playlist.download_all()

Shows the same error

AttributeError: 'Playlist' object has no attribute 'download_all'

Solution

  • You need to get the first stream from the Stream of the YouTube object that you created. If you look at the documentation you could have know this.

    Try this for downloading the video:

    from pytube import YouTube
    url = str(input("Youtube video url :"))
    youtube = YouTube(url)
    youtube.streams.first().download()
    

    For the playlist try this:

    from pytube import Playlist
    playlist = Playlist('https://www.youtube.com/playlist?list=PLRfY4Rc-GWzhdCvSPR7aTV0PJjjiSAGMs')
    print('Number of videos in playlist: %s' % len(playlist.video_urls))
    
    # Loop through all videos in the playlist and download them
    for video in playlist.videos:
        video.streams.first().download()