pytube:AttributeError:“NoneType”对象没有属性“span”

Luk*_*fer 15 python attributeerror pytube

我刚刚下载了 pytube(版本 11.0.1)并从这里开始使用以下代码片段:

from pytube import YouTube
YouTube('https://youtu.be/9bZkp7q19f0').streams.first().download()
Run Code Online (Sandbox Code Playgroud)

这给出了这个错误:

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'
Run Code Online (Sandbox Code Playgroud)

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

jaj*_*arr 17

发现这个问题,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 。


Yal*_*dli 15

编辑:

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

暂时我们再次遇到此错误。您可以尝试这里给出的解决方案

或者

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

你改变

name = re.escape(get_throttling_function_name(js))

name = "hha"

谢谢杰弗里·威廉姆斯

  • 在应用 parser.py avobe提及补丁没有成功后,这对我有用。手指交叉,看看它能持续多久! (4认同)

小智 9

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

你可以这样安装 pytube:

pip install git+https://github.com/baxterisme/pytube
Run Code Online (Sandbox Code Playgroud)

代替:

pip install pytube
Run Code Online (Sandbox Code Playgroud)


ssc*_*lvo 5

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

pip install --upgrade pytube
Run Code Online (Sandbox Code Playgroud)