提问人:user4665278 提问时间:11/14/2023 更新时间:11/14/2023 访问量:27
Pytube 库,在 video.description 中出现错误
Pytube library, getting error in video.description
问:
from pytube import YouTube
video = YouTube('https://www.youtube.com/watch?v=tDKSwtKhteE')
print('Author: ' + video.author)
print('Title: ' + video.title)
print('Description: ' + video.description)
你好!
我正在尝试使用 Python 中的 Pytube 库从 Youtube 视频中获取视频描述。
当我尝试获取描述时,出现此错误
print('Description: ' + video.description)
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
TypeError: can only concatenate str (not "NoneType") to str
Pytube给了我一个“无”,但是,为什么?
有什么想法吗?
谢谢。
这个脚本一年前就可以工作了,现在我有 Pytube v15。我阅读了 Pytube 文档,但我没有发现任何新内容 https://pytube.io/en/latest/api.html#pytube.YouTube.description
答:
1赞
ZCGCoder
11/14/2023
#1
不幸的是,正如本期 PyTube 所述,PyTube 中存在一个错误,使其无法显示描述。唯一的方法是编写另一个函数来获取描述。根据此注释,该函数可以像这样实现:
from json import loads
from pytube import YouTube
def get_description(video: YouTube) -> str:
i: int = video.watch_html.find('"shortDescription":"')
desc: str = '"'
i += 20 # excluding the `"shortDescription":"`
while True:
letter = video.watch_html[i]
desc += letter # letter can be added in any case
i += 1
if letter == '\\':
desc += video.watch_html[i]
i += 1
elif letter == '"':
break
return loads(desc)
把它放在一起:
from pytube import YouTube
from json import loads
def get_description(video: YouTube) -> str:
i: int = video.watch_html.find('"shortDescription":"')
desc: str = '"'
i += 20 # excluding the `"shortDescription":"`
while True:
letter = video.watch_html[i]
desc += letter # letter can be added in any case
i += 1
if letter == '\\':
desc += video.watch_html[i]
i += 1
elif letter == '"':
break
return loads(desc)
video = YouTube('https://www.youtube.com/watch?v=tDKSwtKhteE')
print('Author: ' + video.author)
print('Title: ' + video.title)
print('Description: ' + get_description(video)) # HERE!
评论