Search code examples
pythonpytube

Get playlist URL count using pytube


I'm trying to print how many links there are in the playlist.

Code

from pytube import Playlist

p = Playlist('https://www.youtube.com/playlist?list=PLRmF_eQXS6BnZb0PnIOvpxD6H8F04DfBX')

print(f'Downloading: {p.title}')
print((p.video_urls))
for video in p.videos:
    video.streams.first().download()

Actual output

Downloading: Bristol’s bday playlist
['https://www.youtube.com/watch?v=r7Rn4ryE_w8', 'https://www.youtube.com/watch?v=YhK2NwPIdt4', 'https://www.youtube.com/watch?v=4EQkYVtE-28', 'https://www.youtube.com/watch?v=CpTr4USXjQw', 'https://www.youtube.com/watch?v=dXJHDhKJ9Dw', 'https://www.youtube.com/watch?v=VKC_hzJ3jzg', 'https://www.youtube.com/watch?v=njvA03rMHx4', 'https://www.youtube.com/watch?v=GBvLVesLZmY', 'https://www.youtube.com/watch?v=JucvYrdSIcM']

Issue

But it prints the links, not the count of them. I want this output:

Downloading: Bristol’s bday playlist
9

Solution

  • You can use the len() function to get the number of elements in a list, which in this case would be the number of links in the playlist. Just replace print((p.video_urls)) with print(f'Number of links: {len(p.video_urls)}')

    Something like this:

    from pytube import Playlist
    p = Playlist('https://www.youtube.com/playlist?list=PLRmF_eQXS6BnZb0PnIOvpxD6H8F04DfBX')
    
    print(f'Downloading: {p.title}')
    print(f'Number of links: {len(p.video_urls)}')
    for video in p.videos:
        video.streams.first().download()