pytube: 'NoneType' 对象没有属性 'span'

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

我尝试按照 pytube 示例从 YouTube 下载视频:

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

并立即得到这个错误:

---------------------------------------------------------------------------
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'

请帮助我。就在昨天还好好的!非常感谢!

我自己 运行 遇到了那个错误,尽管它得到了临时修复,但它似乎经常发生。

找到 github 的修复:NoneType object has no attribute 'span'

只需将函数 get_throttling_function_name 替换为:

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"
)