Skip to content

Resolving YouTube 403 Forbidden Errors in Python (Pytube)

Problem Statement

Many Python developers using pytube (version 15.0.0 and earlier) have recently encountered HTTP 403 Forbidden errors when attempting to download YouTube content. This issue began around November 2024 and appears to be caused by YouTube implementing stricter anti-bot measures and changing its API behavior.

The errors typically occur when:

  • Trying to download audio-only streams
  • Using standard pytube download methods
  • Following previous solutions like modifying user agents in innertube.py

WARNING

YouTube regularly updates its anti-bot systems. Libraries that don't maintain active development often stop working unexpectedly with HTTP 403 errors.

Option 1: Switch to pytubefix (Maintained Fork)

pytubefix is an actively maintained fork of pytube with regular updates to handle YouTube's API changes:

  1. Install pytubefix:
bash
pip uninstall pytube  # Remove original pytube
pip install pytubefix
  1. Update your code:
python
from pytubefix import YouTube  # Only import change needed

yt = YouTube('https://youtube.com/watch?v=...')
stream = yt.streams.get_audio_only()
stream.download()

Benefits:

  • Minimal code changes required
  • Maintains pytube's familiar API
  • Actively maintained with recent updates

Option 2: Use yt-dlp (Robust Alternative)

yt-dlp is a powerful, actively maintained fork of youtube-dl with better YouTube compatibility:

  1. Install yt-dlp:
bash
pip install yt-dlp
  1. Basic usage:
python
from yt_dlp import YoutubeDL

URLS = ['https://youtube.com/watch?v=...']

with YoutubeDL() as ydl:
    ydl.download(URLS)  # Downloads best available quality
  1. For audio-only downloads:
python
ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }]
}

with YoutubeDL(ydl_opts) as ydl:
    ydl.download(URLS)

Option 3: Handle Bot Detection in yt-dlp

If you see Sign in to confirm you're not a bot errors with yt-dlp, provide browser cookies:

python
from yt_dlp import YoutubeDL

ydl_opts = {
    'cookiesfrombrowser': ('chrome',),  # Auto-load from Chrome
    # 'cookiefile': 'cookies.txt',   # Alternative: Use exported cookies file
    'outtmpl': 'downloads/%(title)s.%(ext)s'
}

with YoutubeDL(ydl_opts) as ydl:
    ydl.download(URLS)

Cookie Setup

  1. Install the "Get Cookies.txt" browser extension
  2. Sign in to YouTube in your browser
  3. Export cookies as cookies.txt in your script directory

Key Considerations

  • Maintenance Status: Both pytubefix and yt-dlp have active maintainers addressing YouTube API changes
  • Authentication: For age-restricted or private content, cookie authentication is essential
  • Legal Compliance: Ensure your usage complies with YouTube's Terms of Service
  • Rate Limiting: Add delays between downloads to avoid IP blocks
python
# Example with throttling
from yt_dlp import YoutubeDL
import time

URLS = [...]  # Your video list

for url in URLS:
    with YoutubeDL() as ydl:
        ydl.download([url])
    time.sleep(10)  # 10-second delay between downloads

Conclusion

For pytube users facing 403 errors, pytubefix provides the smoothest transition with minimal code changes. For more robust downloads (especially with authentication needs), yt-dlp with cookie support offers the best long-term solution. Both approaches have proven effective against YouTube's recent anti-bot measures.