Search code examples
pythonpytube

Stuck with pytube


Oh my python guys, please help me. One of my python projects suddenly stopped working for no reason. I'm using pytube module and when i try to run the code i get this error:

Traceback (most recent call last):
File "C:\Users\giova\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\contrib\search.py", line 94, in fetch_and_parse
    sections = raw_results['contents']['twoColumnSearchResultsRenderer'][
KeyError: 'twoColumnSearchResultsRenderer'                                                                             fetch_and_parse

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\giova\OneDrive\Desktop\Coding\Python\youtubeapp.py", line 38, in <module>
    videoSearch()
  File "C:\Users\giova\OneDrive\Desktop\Coding\Python\youtubeapp.py", line 21, in videoSearch
    availableResults = len(vid.results)
  File "C:\Users\giova\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\contrib\search.py", line 62, in results
    videos, continuation = self.fetch_and_parse()                                                                      results
  File "C:\Users\giova\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\contrib\search.py", line 97, in fetch_and_parse                                                                                                       fetch_and_parse
    sections = raw_results['onResponseReceivedCommands'][0][
KeyError: 'onResponseReceivedCommands'

This is not even the only error i get, sometimes i got "http error 410: gone" error or some like this. I haven't changed the code for about two weeks (it was working two weeks ago) and it stopped working. I don't know what is happening to my code.

This is the full code:

from pytube import Search, YouTube

print("================================\n What do you want to do?: ")

availableChoose = [
    '1 Search videos', 
    '...', 
    '================================'
]

for choose in availableChoose:
    print(choose)

userChoose = input()

userChoose = userChoose.lower()

def videoSearch():
    userSearch = input("Enter the title of the video you want to search: ")
    vid = Search(userSearch)
    availableResults = len(vid.results)
    strAvailableResults = str(availableResults)
    print("The available results are " + strAvailableResults)
    vidResultsList = vid.results
    vidResultsList = str(vidResultsList)
    vidResultsList = vidResultsList.replace("<pytube.__main__.YouTube object: videoId=", "")
    vidResultsList = vidResultsList.replace(">", "")
    vidResultsList = vidResultsList.replace("[", "")
    vidResultsList = vidResultsList.replace("]", "")
    vidResultsList = vidResultsList.replace(" ", "")
    vidResultsList = vidResultsList.split(',')
    for vidResultsObject in vidResultsList:
        vidLink = ("https://www.youtube.com/watch?v=" + vidResultsObject)
        vidTempObject = YouTube(vidLink)
        print(vidTempObject.title + " - " + vidLink)
        
if(userChoose == "search" or userChoose == "search video" or userChoose == "search videos" or userChoose == "1"):
    videoSearch()

Solution

  • pytube's 11.0.0 API docs list the Search.fetch_and_parse() method.

    The corresponding source-code shows that it internally handles a KeyError for onResponseReceivedCommands:

     # Initial result is handled by try block, continuations by except block
            try:
                sections = raw_results['contents']['twoColumnSearchResultsRenderer'][
                    'primaryContents']['sectionListRenderer']['contents']
            except KeyError:
                sections = raw_results['onResponseReceivedCommands'][0][
                    'appendContinuationItemsAction']['continuationItems']
    

    This method is an inner one, that is used by your vid.results call.

    Might be, that the Youtube API has changed there response and your version of pytube is not fitting anymore.

    Bug already filed

    See pytube issue #1082 and issue #1106.

    Meanwhile use another branch

    tfdahlin's fork has a bugfixed version. It was already proposed as Pull-Request to the project:

    • PR #1090, opened on 2021-08-13 (currently waiting for approval).