pytube: AttributeError: 'NoneType' object has no attribute 'span'

pytube: AttributeError: 'NoneType' object has no attribute 'span'

我刚刚下载了 pytube(版本 11.0.1)并从 here 的代码片段开始:

from pytube import YouTube
YouTube('https://youtu.be/9bZkp7q19f0').streams.first().download()

给出了这个错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-29-0bfa08b87614> in <module>
----> 1 YouTube('https://youtu.be/9bZkp7q19f0').streams.first().download()

~/anaconda3/lib/python3.8/site-packages/pytube/__main__.py in streams(self)
    290         """
    291         self.check_availability()
--> 292         return StreamQuery(self.fmt_streams)
    293 
    294     @property

~/anaconda3/lib/python3.8/site-packages/pytube/__main__.py in fmt_streams(self)
    175         # https://github.com/pytube/pytube/issues/1054
    176         try:
--> 177             extract.apply_signature(stream_manifest, self.vid_info, self.js)                                                                            
    178         except exceptions.ExtractError:
    179             # To force an update to the js file, we clear the cache and retry                                                                           

~/anaconda3/lib/python3.8/site-packages/pytube/extract.py in apply_signature(stream_manifest, vid_info, js)                                                     
    407 
    408     """
--> 409     cipher = Cipher(js=js)
    410 
    411     for i, stream in enumerate(stream_manifest):

~/anaconda3/lib/python3.8/site-packages/pytube/cipher.py in __init__(self, js)
     42 
     43         self.throttling_plan = get_throttling_plan(js)
---> 44         self.throttling_array = get_throttling_function_array(js)
     45 
     46         self.calculated_n = None

~/anaconda3/lib/python3.8/site-packages/pytube/cipher.py in get_throttling_function_array(js)                                                                   
    321 
    322     array_raw = find_object_from_startpoint(raw_code, match.span()[1] - 1)                                                                              
--> 323     str_array = throttling_array_split(array_raw)
    324 
    325     converted_array = []

~/anaconda3/lib/python3.8/site-packages/pytube/parser.py in throttling_array_split(js_array)                                                                    
    156             # Handle functions separately. These can contain commas
    157             match = func_regex.search(curr_substring)
--> 158             match_start, match_end = match.span()
    159 
    160             function_text = find_object_from_startpoint(curr_substring, match.span()[1])

AttributeError: 'NoneType' object has no attribute 'span'

我想知道为什么?谁能帮我?我在 运行 conda 环境中 ipython 控制台(IPython 版本 7.22.0)和 Python 3.8.8 中 运行 这个片段。

发现这个问题,pytube v11.0.1。对我来说有点晚了,但如果明天没有人提交修复,我会检查一下。

C:\Python38\lib\site-packages\pytube\parser.py

更改此行:

152: func_regex = re.compile(r"function\([^)]+\)")

对此:

152: func_regex = re.compile(r"function\([^)]?\)")

问题是正则表达式需要一个带有参数的函数,但我猜 youtube 添加了一些包含非参数化函数的 src。

我遇到了同样的问题,我按照上面的答案更改了 parser.py,只是在 GitHub 上分叉了 pytube 库,并更改了文件。

你可以这样安装pytube:

pip install git+https://github.com/baxterisme/pytube

而不是:

pip install pytube

最简单的解决方案可以是:

  1. 前往 python 的安装位置 (主要在: C:/Users/HP/appdata/local/programs/python)
  2. 在右上角搜索pytube,删除所有看到的pytube模块
  3. 在此处打开您的 cmd(以管理模式)
  4. 输入命令: pip 安装 pytube
  5. 完成了✅ 现在再次尝试 运行 代码 [ ⭐我有同样的问题,这些步骤解决了它]

问题版本是11.0.1,现在已经修复了,所以你只需要升级到较新的版本,就可以再次正常工作了:

pip install --upgrade pytube

编辑:

我试图使我的解决方案保持最新,但我开始觉得 pytube 似乎经常出现问题,这些错误对我的项目产生了不利影响。所以我重构了它,从现在开始使用ytdlp库。因此,在您阅读本文时,此解决方案可能不适合您。

暂时我们又遇到了这个错误。您可以尝试给定 here

的解决方案

或者

确保你在 pytube/cipher.py 的第 293 行:

你变了

name = re.escape(get_throttling_function_name(js))

name = "hha"

谢谢Jeffery Williams