Skip to content
Advertisement

I can’t make a foor loop to put number before video_title and URL in pytube

i just want to give program a url of a playlist and print the title and urls and before titles and urls i want be Ordinal numbers also i new in python

i try for loop and while loop an i get list error

from pytube import YouTube
from pytube import Playlist

SAVE_PATH = "E:/"
i =0
p = Playlist("the play list url")

for i in p:
    print(i + p.title)
    print(i +p.video_urls )
    i += 1

Advertisement

Answer

I think you are missing in “for” loop, you tried to use as index for videos, you can do it using with this way;

from pytube import YouTube
from pytube import Playlist
from pprint import pp

save_path = '.'
my_list = Playlist("https://www.youtube.com/playlist?app=desktop&list=PL4A1F702CEBDEAAA3")
list_details = []

for order, video in enumerate(my_list):
    current_video = YouTube(video)
    list_details.append({
        'order': order+1,
        'title': current_video.title,
        'author': current_video.author
    })
pp(list_details)

here is the sample output;

[{'order': 1, 'title': 'Stars & Stripes', 'author': 'WSB111'},
 {'order': 2, 'title': 'Lincoln Memorial', 'author': 'WSB111'},
 {'order': 3, 'title': 'Spring has Sprung', 'author': 'WSB111'},
 {'order': 4, 'title': 'Purple Monument Majesty', 'author': 'WSB111'},
 {'order': 5, 'title': 'Freedom', 'author': 'WSB111'},
 {'order': 6, 'title': 'American Montage', 'author': 'WSB111'}]
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement