英文:
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(", ")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论