pytube: "urllib.error.HTTPError: HTTP Error 404: Not Found" when downloading using pytube

I’ve been getting this error ever since I downloaded pytube, which was only two weeks ago, but it keeps getting worse. At least 1 in 30 downloads will give me the error “urllib.error.HTTPError: HTTP Error 404: Not Found”

When I run the loop using the same YouTube link and Download location:

import pytube

for i in range(50):
    out_file = pytube.YouTube("https://www.youtube.com/watch?v=yHwGIA4VeOc").streams.first().download("D:\Music")
    print("Done")

I get the error “urllib.error.HTTPError: HTTP Error 404: Not Found” at least once during the loop. Sometimes it will give the error on the 5th download, sometimes on the 43rd. It is random. If I’m lucky it will download them all successfully, but that almost never happens.

I’ve tried putting a delay between downloads and looping through a list of links instead of using the same link for each download, but neither worked.

There is an old issue from 2019, where people have left comments having the same issue within the last two weeks, so I’m not the only one.

Information:

  • Python 3.9.5
  • Pytube 10.8.1
  • I used “python -m pip install git+https://github.com/pytube/pytube” to download pytube

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 48 (1 by maintainers)

Most upvoted comments

Hello, i have the same error and i thought it is my bad. I have created a sentry implement to see the error better. Here is a screenshot what they do. For the first i think my post url is wrong, so pytube dont get any url but they do. Sometime it work and sometime not. like 50/50 For me i looks like grabing the link - works - grabbing video info - works and then try to catch video info again and failed. i dont know why pytube is trying to grab it again. Opera Momentaufnahme_2021-05-19_201901_sentry io

The reason that it looks like it’s making a request twice is because of the 301 redirect. The library only makes the request once, but behind the scenes httplib is making a request to youtube.com/, getting redirected to www.youtube.com, and then proceeding with the request there.

@ the rest of y’all, I’m going to spend tonight seeing if I can get this fixed

Problem is get_video_info , change code in pytube/extract.py

def _video_info_url(params: OrderedDict) -> str:     
    return "https://www.youtube.com/get_video_info?html5=1&video_id=" + params['video_id']

not

def _video_info_url(params: OrderedDict) -> str:    
    return "https://youtube.com/get_video_info?" + urlencode(params) 

PR/996

Not working, I’m also getting the “KeyError: ‘player_response’” error

EDIT: I fixed it by removing [‘player_response’] on line 190 in __main__.py So it should look something like this: if 'streamingData' not in self.player_config_args: But I don’t know if this is a proper fix.

Update: I think I found a way around using the get_video_info url, I’m working on updating supporting functions that relied on this information now.

python -m pip install --upgrade pytube should install the fix now. If there are further issues with v10.8.2, please open a new ticket so they get documented properly

ty for all the tracebacks, it looks like it is an issue with just the vid info url, I’ll see if I can investigate what’s causing it sometime this week

Huge thank you to @Ssuwani for the proposed solution, and @drwjf for adapting it to the existing code convention while I was unavailable

Problem is get_video_info , change code in pytube/extract.py

def _video_info_url(params: OrderedDict) -> str:     
    return "https://www.youtube.com/get_video_info?html5=1&video_id=" + params['video_id']

not

def _video_info_url(params: OrderedDict) -> str:    
    return "https://youtube.com/get_video_info?" + urlencode(params) 

PR/996

Solution works for me. I saw some responses with “player_response” being an error , just be sure you have this : return “https://www.youtube.com/get_video_info?html5=1&video_id=” + params[‘video_id’] Notice the lack of “amp;” , if you have that the reponse comes back as “status: fail” and hence no player_response in returned response

Not working, I’m also getting the “KeyError: ‘player_response’” error

EDIT: I fixed it by removing [‘player_response’] on line 190 in __main__.py So it should look something like this: if 'streamingData' not in self.player_config_args: But I don’t know if this is a proper fix.

Ssuwani fix +This fixed it for me

Still in progress, have to sleep for now. Currently running into a 403 issue if anybody wants to check out my branch to try and solve it. Otherwise I’ll try resume work on this tomorrow afternoon

Can we please re-open this issue as it seems that it has not been fully resolved ? I’m facing the same problem as @SergeZ23. Thanks in advance!

I get the same error on certain videos with pytube version 10.8.4

Traceback (most recent call last):
File "C:\Users\Serge\PycharmProjects\Youtube\venv\lib\site-packages\pytube\streams.py", line 261, in download
  max_retries=max_retries
File "C:\Users\Serge\PycharmProjects\Youtube\venv\lib\site-packages\pytube\request.py", line 160, in stream
  timeout=timeout
File "C:\Users\Serge\PycharmProjects\Youtube\venv\lib\site-packages\pytube\request.py", line 36, in _execute_request
  return urlopen(request, timeout=timeout)  # nosec
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen
  return opener.open(url, data, timeout)
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
  response = meth(req, response)
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
  'http', request, response, code, msg, hdrs)
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 569, in error
  return self._call_chain(*args)
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
  result = func(*args)
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 649, in http_error_default
  raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/Serge/PycharmProjects/Youtube/Download.py", line 161, in <module>
  video_test('https://www.youtube.com/watch?v=dCqHdHKvSos')
File "C:/Users/Serge/PycharmProjects/Youtube/Download.py", line 154, in video_test
  video_download(video)
File "C:/Users/Serge/PycharmProjects/Youtube/Download.py", line 87, in video_download
  stream.download(output_path=complete_name, filename='VIDEO_' + title)
File "C:\Users\Serge\PycharmProjects\Youtube\venv\lib\site-packages\pytube\streams.py", line 274, in download
  max_retries=max_retries
File "C:\Users\Serge\PycharmProjects\Youtube\venv\lib\site-packages\pytube\request.py", line 127, in seq_stream
  yield from stream(url, timeout=timeout, max_retries=max_retries)
File "C:\Users\Serge\PycharmProjects\Youtube\venv\lib\site-packages\pytube\request.py", line 160, in stream
  timeout=timeout
File "C:\Users\Serge\PycharmProjects\Youtube\venv\lib\site-packages\pytube\request.py", line 36, in _execute_request
  return urlopen(request, timeout=timeout)  # nosec
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen
  return opener.open(url, data, timeout)
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
  response = meth(req, response)
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
  'http', request, response, code, msg, hdrs)
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 569, in error
  return self._call_chain(*args)
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
  result = func(*args)
File "C:\Users\Serge\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 649, in http_error_default
  raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

I’m downloading streams that are not progressive, sometimes it would download the video stream and fail on the audio, sometimes it would fail on the video stream. I’ve also seen a case where on the first try I was able to download the video stream and failed on the audio, but on the second try with the same video, the error has occured while trying to download the video stream that has been downloaded successfully before that. The code is:

   stream = yt.streams.filter(file_extension='mp4', res='1080p').first()
    stream_audio = yt.streams.filter(file_extension='mp4', only_audio=True).order_by('abr').last()
    stream.download(output_path=complete_name, filename='VIDEO_' + title)
    stream_audio.download(output_path=complete_name, filename='AUDIO_' + title)

python -m pip install --upgrade pytube should install the fix now. If there are further issues with v10.8.2, please open a new ticket so they get documented properly

Solved my problem, thanks friend

Since somebody opened a new issue for the current 404 problems, I’m going to leave this issue closed and use that one instead.

Hey folks; I’m going to merge the PR @drwjf made once the CI tests pass. Expect v10.8.2 to be on pypi in the next 10 minutes or so.

works not for me, get error ‘player respone’ 😦

@tfdahlin I’ve been getting the same error using the CLI (so I’m not looping through multiple urls at once like @FlubOtic), but the error still seems to occur randomly, and I’ve found that if I try to download the same video a few times in a row the download will eventually succeed (after failing with the above error on the first couple of tries).

Can confirm too.

Full traceback:

Traceback (most recent call last):
  File "/Users/victorrolandosanchezjara/Programming/youtube-downloader-python/download.py", line 8, in <module>
    print('Downloading video - ', current_video, ' :', yt.title)
  File "/usr/local/anaconda3/lib/python3.7/site-packages/pytube/__main__.py", line 351, in title
    self._title = self.player_response['videoDetails']['title']
  File "/usr/local/anaconda3/lib/python3.7/site-packages/pytube/__main__.py", line 167, in player_response
    if isinstance(self.player_config_args["player_response"], str):
  File "/usr/local/anaconda3/lib/python3.7/site-packages/pytube/__main__.py", line 187, in player_config_args
    self._player_config_args = self.vid_info
  File "/usr/local/anaconda3/lib/python3.7/site-packages/pytube/__main__.py", line 278, in vid_info
    return dict(parse_qsl(self.vid_info_raw))
  File "/usr/local/anaconda3/lib/python3.7/site-packages/pytube/__main__.py", line 108, in vid_info_raw
    self._vid_info_raw = request.get(self.vid_info_url)
  File "/usr/local/anaconda3/lib/python3.7/site-packages/pytube/request.py", line 52, in get
    response = _execute_request(url, headers=extra_headers, timeout=timeout)
  File "/usr/local/anaconda3/lib/python3.7/site-packages/pytube/request.py", line 36, in _execute_request
    return urlopen(request, timeout=timeout)  # nosec
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 563, in error
    result = self._call_chain(*args)
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 755, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/local/anaconda3/lib/python3.7/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

My code is quite simple. Downloading a list of videos.

from tqdm import tqdm
from pytube import YouTube

with open('videos_list.txt') as videos_list:
    current_video = 1
    for url in tqdm(videos_list, desc='Downloading videos'):
        yt = YouTube(url)
        print('Downloading video - ', current_video, ' :', yt.title)
        yt.streams.first().download('./videos/'+yt.title)
        print('Finished downloading video - ', current_video, ' :', yt.title)
        current_video += 1