0

I am trying to use pytube (v15.0.0) to fetch the titles of YouTube videos. However, for every video I try, my script fails with the same error: HTTP Error 400: Bad Request.

I have already updated pytube to the latest version by running pip install --upgrade pytube, but the problem persists.

Minimal, Reproducible Example

Here is the smallest possible script that demonstrates this problem. It only uses pytube and has no other dependencies. Anyone can copy and run this code to see the error.

import logging
from pytube import YouTube

# Configure logging to see the error clearly
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def fetch_video_title(video_id):
    """
    Attempts to fetch a single video title using pytube.
    This function consistently fails.
    """
    video_url = f"https://www.youtube.com/watch?v={video_id}"
    logging.info(f"Attempting to fetch title for: {video_url}")
    
    try:
        # Create a YouTube object
        yt = YouTube(video_url)
        
        # Accessing the .title attribute triggers the network request
        title = yt.title
        
        logging.info(f"SUCCESS: Title is '{title}'")
        return title
    except Exception as e:
        # The script always ends up here
        logging.error(f"FAILURE for video ID '{video_id}': {e}")
        return None

# Main execution block to run the test
if __name__ == "__main__":
    # A list of standard YouTube video IDs to test
    video_ids_to_test = ["G_Ttz9Dp5lI", "n2f2MPDScdc"]
    
    print("--- Starting Pytube Title Fetch Test ---")
    for video_id in video_ids_to_test:
        fetch_video_title(video_id)
    print("\n--- Test Finished ---")

What I Get (Actual Output):

When I run the code above, the output clearly shows the HTTP Error 400 for each attempt:

--- Starting Pytube Title Fetch Test ---
2025-08-14 12:30:00 - INFO - Attempting to fetch title for: https://www.youtube.com/watch?v=G_Ttz9Dp5lI
2025-08-14 12:30:02 - ERROR - FAILURE for video ID 'G_Ttz9Dp5lI': HTTP Error 400: Bad Request
2025-08-14 12:30:02 - INFO - Attempting to fetch title for: https://www.youtube.com/watch?v=n2f2MPDScdc
2025-08-14 12:30:04 - ERROR - FAILURE for video ID 'n2f2MPDScdc': HTTP Error 400: Bad Request

--- Test Finished ---

What I Expect:

I expect the script to successfully connect to YouTube and print the title for each video, like this:

--- Starting Pytube Title Fetch Test ---
2025-08-14 12:30:00 - INFO - Attempting to fetch title for: https://www.youtube.com/watch?v=G_Ttz9Dp5lI
2025-08-14 12:30:02 - INFO - SUCCESS: Title is '[Actual Video Title Here]'
...
--- Test Finished ---

My Question:

Given that I am using the latest version of pytube and my code is very simple, is this HTTP 400 error a known, temporary issue with the library (e.g., YouTube changed something that broke pytube), or is there a mistake in my code that I am missing?

1 Answer 1

1

On pypi.org you can see that latest version 15.0.0 was created May 7, 2023 so it is 2 years old.
On GitHub you can see that last update on Pytube repo also was 2 years ago.

So it may not work. Youtube can change webpage every month.


You may try with module yt-dlp which last version was created Aug 11, 2025 - it is 3 days ago.

On ytp-dl repo you may see how to use it as standalone program

yt-dlp --get-title https://www.youtube.com/watch?v=G_Ttz9Dp5lI

But there are also Embedding examples which I use in this example

import json
import yt_dlp

URL = 'https://www.youtube.com/watch?v=G_Ttz9Dp5lI'

# โ„น๏ธ See help(yt_dlp.YoutubeDL) for a list of available options and public functions
ydl_opts = {
    'quiet': True,      # Don't print messages to stdout
}

with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    info = ydl.extract_info(URL, download=False)

    # โ„น๏ธ ydl.sanitize_info makes the info json-serializable
    #print(json.dumps(ydl.sanitize_info(info), indent=2))

    print('title:', info['title'])

BTW:

accidently I found that there is also ytp-dl with the same date which has no link to repo and maybe it is fake or malware.

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.