pytube.exceptions.RegexMatchError:get_transform_object:找不到 var for={(.*?)} 的匹配项;

Haj*_*ndt 5 python youtube pytube

使用以下代码使用 PyTube 库下载视频时:

yt.streams.get_highest_resolution().download("PATH", f"PATH.mp4")
Run Code Online (Sandbox Code Playgroud)

我收到错误:

raise RegexMatchError(caller="get_transform_object", pattern=pattern)
pytube.exceptions.RegexMatchError: get_transform_object: could not find match for var for={(.*?)};
Run Code Online (Sandbox Code Playgroud)

我在 Stack Overflow 和 PyTube 的 Git 存储库中看到了很多修复,但它们似乎涉及cypher.py. 我想知道如何交替get_transform_object班级cypher.py以匹配正则表达式检查。

Ham*_*ywa 10

这是库更新期间的快速修复。

-> 在文件 .venv/lib/python3.10/site-packages/pytube/cipher.py 中,我使用的是 python 3.10,我的虚拟环境称为 .venv 您只需找到库 pytube 并转到文件 cipher。 py 并暂时编辑其源代码。

-> 找到方法 get_transform_object 并将其替换为如下

def get_transform_object(js: str, var: str) -> List[str]:
    pattern = r"var %s={(.*?)};" % re.escape(var)
    logger.debug("getting transform object")
    regex = re.compile(pattern, flags=re.DOTALL)
    transform_match = regex.search(js)
    
    if not transform_match:
        # i commented out the line raising the error
        # raise RegexMatchError(caller="get_transform_object", pattern=pattern)
        return []  # Return an empty list if no match is found

    return transform_match.group(1).replace("\n", " ").split(", ")
Run Code Online (Sandbox Code Playgroud)