Search code examples
pythonpython-3.xpytube

pytube: 'NoneType' object has no attribute 'span'


I try to follow pytube example for downloading video from YouTube:

from pytube import YouTube
video = YouTube('https://www.youtube.com/watch?v=BATOxzbVNno')
video.streams.all()

and immediately get this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-2556eb2eb903> in <module>()
      1 from pytube import YouTube
      2 video = YouTube('https://www.youtube.com/watch?v=BATOxzbVNno')
----> 3 video.streams.all()

5 frames
/usr/local/lib/python3.7/dist-packages/pytube/cipher.py in get_throttling_function_code(js)
    301     # Extract the code within curly braces for the function itself, and merge any split lines
    302     code_lines_list = find_object_from_startpoint(js, match.span()[1]).split('\n')
--> 303     joined_lines = "".join(code_lines_list)
    304 
    305     # Prepend function definition (e.g. `Dea=function(a)`)

AttributeError: 'NoneType' object has no attribute 'span'

Please help me. It worked fine just yesterday! Thanks a lot!


Solution

  • Just ran into that error myself, seems it occurs quite frequently regardless of it getting temporary fixes.

    Found a fix on github: NoneType object has no attribute 'span'

    Just replace the function get_throttling_function_name with:

    def get_throttling_function_name(js: str) -> str:
    """Extract the name of the function that computes the throttling parameter.
    
    :param str js:
        The contents of the base.js asset file.
    :rtype: str
    :returns:
        The name of the function used to compute the throttling parameter.
    """
    function_patterns = [
        # https://github.com/ytdl-org/youtube-dl/issues/29326#issuecomment-865985377
        # a.C&&(b=a.get("n"))&&(b=Dea(b),a.set("n",b))}};
        # In above case, `Dea` is the relevant function name
        r'a\.[A-Z]&&\(b=a\.get\("n"\)\)&&\(b=([^(]+)\(b\)',
    ]
    logger.debug('Finding throttling function name')
    for pattern in function_patterns:
        regex = re.compile(pattern)
        function_match = regex.search(js)
        if function_match:
            logger.debug("finished regex search, matched: %s", pattern)
            function_name = function_match.group(1)
            is_Array = True if '[' or ']' in function_name else False
            if is_Array:
                index = int(re.findall(r'\d+', function_name)[0])
                name = function_name.split('[')[0]
                pattern = r"var %s=\[(.*?)\];" % name
                regex = re.compile(pattern)
                return regex.search(js).group(1).split(',')[index]
            else:
                return function_name
    
    raise RegexMatchError(
        caller="get_throttling_function_name", pattern="multiple"
    )