top of page

Loop over YouTube playlists with Python and get video title, likes, views, and duration.

Here is a Python script that allows iterates over a YouTube playlists. The current limitation is that I have to prepare a dictionary with Playlist title and link, I am still trying to figure out how to scrape all the playlists in a channel without using Selenium, but this is something that will work for now.

import pafy
from pytube import Playlist
import pandas as pd

# pip install pytube
# pip install pafy
# pip install pandas
# pip install youtube-dl==2020.12.2

all_playlist_urls = {
    'Power Automate Tutorials': 'https://www.youtube.com/playlist?list=PL1myWUzvmmDHFbyPMYLeA3qUZNXGurVWX',
    'Microsoft Excel Tutorials': 'https://www.youtube.com/playlist?list=PL1myWUzvmmDE20xWCuZsUxcB1Y5DfKlpm',
    'Python Tutorials': 'https://www.youtube.com/playlist?list=PL1myWUzvmmDFWkIMzqtzprR9rMo7PheOL'
}
result = []

for playlist_title, playlist_url in all_playlist_urls.items():
    playlist = Playlist(playlist_url)
    
    for video_url in playlist:
        video = pafy.new(video_url)
        result.append(
            [
                playlist_title,
                video.title, 
                video.viewcount, 
                video.likes, 
                video.dislikes, 
                video.duration, 
                video.videoid,
                video_url,
                playlist_url
            ]
        )

df  = pd.DataFrame(
    result, 
    columns = ['Playlist','Title', 'Views', 'Likes', 'Dislikes', 'Duration', 'VideoID', 'URL', 'PlaylistURL']
)
print(df)


554 views0 comments
bottom of page