Skip to content
Related Articles

Related Articles

Improve Article

Python | Program to download complete Youtube playlist

  • Last Updated : 06 Mar, 2019

Python is a multi-purpose programming language and widely used for scripting small tasks. Let’s see how to make your own Youtube Playlist Downloader using Python. Though there are many software available in market but making your own serving that purpose is quite a learning and impressive as well.

Modules needed –

BeautifulSoup bs4
sys module
urllib module
pytube module

How it works ?
From the given URL of a YouTube playlist, our program will perform web scraping and fetch all the YouTube video links and append it under a links array. Then using the pytube library we will download the corresponding YouTube videos from the link in the links array. The parameters for downloading the YouTube video (quality, mime_type, etc) can be specified in streams constructor. The videos will be downloaded with the name of the original video.

Let’s see the code:

# Importing libraries
import bs4 as bs
import sys
import urllib.request
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
import pytube # library for downloading youtube videos
class Page(QWebEnginePage):
    def __init__(self, url): = QApplication(sys.argv)
        self.html = ''
    def _on_load_finished(self):
        self.html = self.toHtml(self.Callable)
        print('Load finished')
    def Callable(self, html_str):
        self.html = html_str
links = []
def exact_link(link):
    vid_id = link.split('=')
    # print(vid_id)
    str = ""
    for i in vid_id[0:2]:
        str += i + "="
    str_new = str[0:len(str) - 1]
    index = str_new.find("&")
    new_link = "" + str_new[0:index]
    return new_link
# Scraping and extracting the video
# links from the given playlist url
page = Page(url)
count = 0
soup = bs.BeautifulSoup(page.html, 'html.parser')
for link in soup.find_all('a', id='thumbnail'):
    # not using first link because it is
    # playlist link not particular video link
    if count == 0
        count += 1
        vid_src = link['href']
        # print(vid_src)
        # keeping the format of link to be
        # given to pytube otherwise in some cases
        new_link = exact_link(vid_src) 
        # error might occur due to this
        # print(new_link)
        # appending the link to the links array
# print(links)
# downloading each video from
# the link in the links array
for link in links: 
    yt = pytube.YouTube(link)
    # Downloaded video will be the best quality video
    stream = yt.streams.filter(progressive=True,
        # printing the links downloaded
        print("Downloaded: ", link) 
        print('Some error in downloading: ', link)


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :