在尝试运行我的小测试脚本时,每十次中有五次Pytube会向我发送此错误。
下面是剧本:
import pytube
import urllib.request
from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=3NCyD3XoJgM')
print('Youtube video title is: ' + yt.title + '! Downloading now!')我得到的是:
Traceback (most recent call last):
File "youtube.py", line 6, in <module>
yt = YouTube('https://www.youtube.com/watch?v=3NCyD3XoJgM')
File "C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pytube\__main__.py", line 91, in __init__
self.prefetch()
File "C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pytube\__main__.py", line 183, in prefetch
self.js_url = extract.js_url(self.watch_html)
File "C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pytube\extract.py", line 143, in js_url
base_js = get_ytplayer_config(html)["assets"]["js"]
KeyError: 'assets'我很困惑。我试图重新安装Python pytube,但是我似乎无法解决这个问题。越来越令人费解的是,剧本一半的时间都在工作,而另一半时间却没有。
发布于 2020-10-28 12:04:28
就目前而言,100%地修正了这一点:
https://github.com/nficano/pytube/pull/767#issuecomment-716184994
如果其他人收到此错误或问题,请在终端或cmd:python -m pip install git+https://github.com/nficano/pytube中运行以下命令
对pytubeX的更新还没有随pip安装一起发布。GitHub链接是解释这种情况的当前开发工具。
发布于 2020-10-26 16:09:54
我也遇到了同样的麻烦,但我保证最上面的答案不会解决任何问题,只要把问题隐藏起来,直到它再次弹出。我研究了"extract.py“文件的这个范围,发现了一个错误。此范围通过字典搜索在Youtube页面的源代码中搜索“字符串”片段,例如:
#Example ---------------
Vars = {
'name':'luis'
'age':'27'
}
print(Vars['name'])
result: 'luis'
#Extract.py Code -------
def js_url(html: str) -> str:
"""Get the base JavaScript url.
Construct the base JavaScript url, which contains
the decipher
"transforms".
:param str html:
The html contents of the watch page.
"""
base_js = get_ytplayer_config(html)["assets"]["js"]
return "https://youtube.com" + base_js错误:
base_js = get_ytplayer_config(html)["assets"]["js"]
KeyError: 'assets'这是因为源代码的这个片段不支持dicionario搜索,所以“KeyError”键错误,因为“资产”不是有效的键,而且源代码不是字典。所以我做了这个剧本,我相信它取代了原来的剧本,但在我的剧本里,特别是出现了其他的错误。
def js_url(html: str) -> str:
"""Get the base JavaScript url.
Construct the base JavaScript url, which contains
the decipher
"transforms".
:param str html:
The html contents of the watch page.
"""
base_js = html[html.find('js') + 4:html.find('.js')
+ 4]
return "https://youtube.com" + base_js上面的脚本搜索函数想要的字符串,而不是字典。
我希望我为更完整的未来解决方案作出了贡献:)
发布于 2020-10-29 06:13:40
将此函数添加到extract.py中
def get_ytplayer_js(html: str) -> Any:
"""Get the YouTube player base JavaScript path.
:param str html
The html contents of the watch page.
:rtype: str
:returns:
Path to YouTube's base.js file.
"""
js_url_patterns = [
r"\"jsUrl\":\"([^\"]*)\"",
]
for pattern in js_url_patterns:
regex = re.compile(pattern)
function_match = regex.search(html)
if function_match:
logger.debug("finished regex search, matched: %s", pattern)
yt_player_js = function_match.group(1)
return yt_player_js
raise RegexMatchError(
caller="get_ytplayer_js", pattern="js_url_patterns"
)并将extract.py中的函数“extract.py”更改为:
def js_url(html: str) -> str:
"""Get the base JavaScript url.
Construct the base JavaScript url, which contains the decipher
"transforms".
:param str html:
The html contents of the watch page.
"""
base_js = get_ytplayer_config(html)["assets"]["js"]
return "https://youtube.com" + base_js至:
def js_url(html: str) -> str:
"""Get the base JavaScript url.
Construct the base JavaScript url, which contains the decipher
"transforms".
:param str html:
The html contents of the watch page.
"""
base_js = get_ytplayer_js(html)
return "https://youtube.com" + base_jshttps://stackoverflow.com/questions/64492922
复制相似问题