python Pytube库,在视频中获取错误,描述

2w3rbyxf  于 11个月前  发布在  Python
关注(0)|答案(1)|浏览(92)
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

e0bqpujr

e0bqpujr1#

不幸的是,正如PyTube的this问题中所述,PyTube中存在一个bug,使其无法显示描述。唯一的方法是编写另一个函数来获取描述。根据此评论,该函数可以像这样实现:

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!

相关问题