为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

蝴蝶不菲 2023-12-29 10:27:57
当尝试运行我的小型测试脚本时,Pytube 十分之五会向我发送此错误。这是脚本:import pytubeimport urllib.requestfrom pytube import YouTubeyt = 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 但我似乎无法解决这个问题。越来越令人困惑的是,该脚本一半的时间有效,但另一半则无效。
查看完整描述

6 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

现在已经100%修复了:

https://github.com/nficano/pytube/pull/767#issuecomment-716184994

如果其他人遇到此错误或问题,请在终端或 cmd 中运行此命令: python -m pip install git+https://github.com/nficano/pytube

尚未随 pip 安装一起发布的 pytubeX 更新。GitHub 链接是当前开发人员对情况的解释。


查看完整回答
反对 回复 2023-12-29
?
回首忆惘然

TA贡献1847条经验 获得超11个赞

我也遇到了同样的麻烦,但我保证最上面的答案不能解决任何问题,只是隐藏问题,直到它再次出现。我调查了“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'

之所以给出这个源代码片段,是因为该源代码片段不支持字典搜索,因此出现 'KeyError' 键错误,因为 'assets' 不是有效键,并且源代码不是字典。所以我做了这个脚本,我相信它取代了原来的脚本,但在我的脚本中,特别是出现了其他错误。


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

上面的脚本搜索函数想要的字符串形式,而不是字典形式。


我希望我为未来更完整的解决方案做出了贡献:)


查看完整回答
反对 回复 2023-12-29
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

将此函数添加到 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 中的函数“js_url”更改为:


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_js


查看完整回答
反对 回复 2023-12-29
?
繁华开满天机

TA贡献1816条经验 获得超4个赞

看来 Pytube 模块已更新。

它适用于 pytube 包

即尝试pip install pytube卸载 pytube 变体


查看完整回答
反对 回复 2023-12-29
?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

我遇到了同样的问题,更新 pytube到当前可用的最新版本问题消失了。

pip install pytube==10.0.0

或者

pip install --upgrade pytube


查看完整回答
反对 回复 2023-12-29
?
慕桂英546537

TA贡献1848条经验 获得超10个赞

如果您正在使用该软件包pytubepytube3,我建议您卸载它并安装pytubeX。无需更改导入。我发现它的工作更加可靠。

编辑:从评论中,如果这些都不起作用,请尝试pytube4

编辑:pytube现在再次维护!


查看完整回答
反对 回复 2023-12-29
  • 6 回答
  • 0 关注
  • 73 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信