'How to download a youtube video using Python? [closed]

I'm trying to download a YouTube video using it's url in python but I'm getting:

urllib.error.HTTPError: HTTP Error 403: Forbidden

I'm using the Python library pytube to download the youtube video. Here is my code:

#importing the module 
from pytube import YouTube 
  
my=YouTube("https://www.youtube.com/watch?v=R4em3LKQCAQ").streams.first()
my.download()

I'm getting the error like below

raise HTTPError(req.full_url, code, msg, hdrs, fp)

urllib.error.HTTPError: HTTP Error 403: Forbidden



Solution 1:[1]

Update 2022 I no longer Maintains mhyt library.

you can try youtube_dl option:

To install:

pip install youtube_dl
# or for faster download and more improvements: 
#pip install yt-dlp

And use:

from youtube_dl import YoutubeDL
# or for yt_dlp: 
# from yt_dlp import YoutubeDL
with YoutubeDL() as ydl:
    ydl.download(["https://www.youtube.com/watch?v=R4em3LKQCAQ"])

ORIGINAL ANSWER:

I have created a library that makes it.

To install:

bash $ pip install mhyt

And use it like:

from mhyt import yt_download
yt_download("https://www.youtube.com/watch?v=R4em3LKQCAQ","download.mp4")

Solution 2:[2]

I did the below way in Windows 10 for downloading youtube videos using Python

  1. Download youtube-dl using

    pip install youtube-dl

or

pip3 install youtube-dl
  1. In Python shell import subprocess like below

    >> import subprocess

    >> cmd = 'youtube-dl -o my_video '+'https://www.youtube.com/watch?v=jzD_yyEcp0M'

    >> subprocess.call(cmd, shell = True)

Hope it will work for you also.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2