Pytube 仅定期工作(KeyError:'assets')

当尝试运行我的小型测试脚本时,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 但我似乎无法解决这个问题。越来越令人困惑的是,该脚本一半的时间有效,但另一半则无效。


蝴蝶不菲
浏览 173回答 6
6回答

慕斯王

现在已经100%修复了:https://github.com/nficano/pytube/pull/767#issuecomment-716184994如果其他人遇到此错误或问题,请在终端或 cmd 中运行此命令:&nbsp;python -m pip install git+https://github.com/nficano/pytube尚未随 pip 安装一起发布的 pytubeX 更新。GitHub 链接是当前开发人员对情况的解释。

回首忆惘然

我也遇到了同样的麻烦,但我保证最上面的答案不能解决任何问题,只是隐藏问题,直到它再次出现。我调查了“extract.py”文件的范围,发现了一个错误。该范围通过字典搜索在视频所在的 Youtube 页面的源代码中搜索“字符串”片段,例如:#Example ---------------Vars = {&nbsp; &nbsp; 'name':'luis'&nbsp; &nbsp; '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&nbsp;the decipher"transforms".:param str html:&nbsp; &nbsp; 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'之所以给出这个源代码片段,是因为该源代码片段不支持字典搜索,因此出现 'KeyError' 键错误,因为 'assets' 不是有效键,并且源代码不是字典。所以我做了这个脚本,我相信它取代了原来的脚本,但在我的脚本中,特别是出现了其他错误。def js_url(html: str) -> str:"""Get the base JavaScript url.Construct the base JavaScript url, which contains&nbsp;the decipher"transforms".:param str html:&nbsp; &nbsp; The html contents of the watch page."""base_js = html[html.find('js') + 4:html.find('.js')&nbsp;+ 4]return "https://youtube.com" + base_js上面的脚本搜索函数想要的字符串形式,而不是字典形式。我希望我为未来更完整的解决方案做出了贡献:)

慕姐4208626

将此函数添加到 extract.pydef get_ytplayer_js(html: str) -> Any:&nbsp; &nbsp; """Get the YouTube player base JavaScript path.&nbsp; &nbsp; :param str html&nbsp; &nbsp; The html contents of the watch page.&nbsp; &nbsp; :rtype: str&nbsp; &nbsp; :returns:&nbsp; &nbsp; Path to YouTube's base.js file.&nbsp; &nbsp; """&nbsp; &nbsp; js_url_patterns = [&nbsp; &nbsp; &nbsp; &nbsp; r"\"jsUrl\":\"([^\"]*)\"",&nbsp; &nbsp; ]&nbsp; &nbsp; for pattern in js_url_patterns:&nbsp; &nbsp; &nbsp; &nbsp; regex = re.compile(pattern)&nbsp; &nbsp; &nbsp; &nbsp; function_match = regex.search(html)&nbsp; &nbsp; &nbsp; &nbsp; if function_match:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.debug("finished regex search, matched: %s", pattern)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yt_player_js = function_match.group(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return yt_player_js&nbsp; &nbsp; raise RegexMatchError(&nbsp; &nbsp; &nbsp; &nbsp;caller="get_ytplayer_js", pattern="js_url_patterns"&nbsp; &nbsp; )并将 extract.py 中的函数“js_url”更改为:def js_url(html: str) -> str:&nbsp; &nbsp; """Get the base JavaScript url.&nbsp; &nbsp; Construct the base JavaScript url, which contains the decipher&nbsp; &nbsp; "transforms".&nbsp; &nbsp; :param str html:&nbsp; &nbsp; &nbsp; &nbsp; The html contents of the watch page.&nbsp; &nbsp; """&nbsp; &nbsp; base_js = get_ytplayer_config(html)["assets"]["js"]&nbsp; &nbsp; return "https://youtube.com" + base_js到:def js_url(html: str) -> str:&nbsp; &nbsp; """Get the base JavaScript url.&nbsp; &nbsp; Construct the base JavaScript url, which contains the decipher&nbsp; &nbsp; "transforms".&nbsp; &nbsp; :param str html:&nbsp; &nbsp; &nbsp; &nbsp; The html contents of the watch page.&nbsp; &nbsp; """&nbsp; &nbsp; base_js = get_ytplayer_js(html)&nbsp; &nbsp; return "https://youtube.com" + base_js

繁华开满天机

看来 Pytube 模块已更新。它适用于 pytube 包即尝试pip install pytube卸载 pytube 变体

慕桂英3389331

我遇到了同样的问题,更新&nbsp;pytube到当前可用的最新版本问题消失了。pip&nbsp;install&nbsp;pytube==10.0.0或者pip&nbsp;install&nbsp;--upgrade&nbsp;pytube

慕桂英546537

如果您正在使用该软件包pytube或pytube3,我建议您卸载它并安装pytubeX。无需更改导入。我发现它的工作更加可靠。编辑:从评论中,如果这些都不起作用,请尝试pytube4编辑:pytube现在再次维护!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python