Search code examples
pythonyoutubepytube

List YouTube playlist URLs using pytube python


I have searched for the topic and I am trying to find a way to list the URLs or links of specific playist. Here's what I got but didn't work for me

import re
from pytube import Playlist

playlist = Playlist("https://www.youtube.com/playlist?list=PL6gx4Cwl9DGCkg2uj3PxUWhMDuTw3VKjM")
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for url in playlist.video_urls:
   print(url)

I got the number of videos 0. Any idea how to make this code work?

And this is another code that is supposed to download all the links from the playlist

from pytube import Playlist
 
try:
    playlist = Playlist('https://www.youtube.com/playlist?list=PLcow8_btriE11hzMbT3-B1sBg4YIc-9g_') 
    playlist.download_all(download_path='./Videos')
except Exception as e:
    print(e)

But I got this 'Playlist' object has no attribute 'download_all'


Solution

  • This works well for me

    from pytube import Playlist
    
    playlist = Playlist('https://www.youtube.com/watch?v=EWdNhA5prXQ&list=PLRhWUdOnZHklr5GcNBO7dDsTspk85M8eH')
    print('Number Of Videos In playlist: %s' % len(playlist.video_urls))
    
    for video in playlist.videos:
        video.streams.first().download()