pytube.exceptions.RegexMatchError: get_transform_object: could not find match for var for={(.*?)};

huangapple go评论75阅读模式
英文:

pytube.exceptions.RegexMatchError: get_transform_object: could not find match for var for={(.*?)};

问题

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

yt.streams.get_highest_resolution().download("路径", f"路径.mp4")

我遇到了一个错误:

raise RegexMatchError(caller="get_transform_object", pattern=pattern)
pytube.exceptions.RegexMatchError: get_transform_object: 无法找到与 var for={(.*?)}; 匹配的内容;

我在Stack Overflow和PyTube的Git存储库中看到了很多修复方法,但它们似乎涉及到cypher.py的不同部分。我想知道如何修改cypher.py中的get_transform_object类以匹配正则表达式检查。

英文:

While downloading a video using the PyTube library using this code:

yt.streams.get_highest_resolution().download("PATH", f"PATH.mp4")

I get an error:

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

I've seen a lot of fixes on Stack Overflow and in the Git repository of PyTube, but they seem to go into different parts of cypher.py. I would like to know how I could alternate get_transform_object class in cypher.py to match the RegEx check.

答案1

得分: 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:
        # 我注释掉了引发错误的那一行
        # raise RegexMatchError(caller="get_transform_object", pattern=pattern)
        return []  # 如果没有匹配项,则返回空列表

    return transform_match.group(1).replace("\n", " ").split(", ")
英文:

Here is a quick fix in the meantime as the library makes an update.

-> In file .venv/lib/python3.10/site-packages/pytube/cipher.py
I am using python 3.10 and my virtual environment is called .venv
You just have to find the library pytube and go to the file cipher.py and edit its source code for now.

-> Find the method get_transform_object and replace it as below

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

huangapple
  • 本文由 发表于 2023年7月17日 19:41:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定