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
CodePudding user response:
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'}]
CodePudding user response:
Use the built-in function enumerate():
for i, song in enumerate(p):
print(i, song.title)
you should also use descriptive names like playlist
and song
instead of p and i. i
is most often just used for the Index.
I cant test the code right now but something along these lines this should work. Let me know if you need any more help.