PyTube3播放列表返回空列表

时间:2020-06-30 16:24:17

标签: python-3.x youtube pytube

似乎在过去2或3周的某个时间,Playlist类似乎已停止为我工作。我尝试了以下来自their GitHub的代码段:

from pytube import Playlist
playlist = Playlist("https://www.youtube.com/playlist?list=PLynhp4cZEpTbRs_PYISQ8v_uwO0_mDg_X")

print(len(playlist.video_urls))
for url in playlist.video_urls:
    print(url)

我尝试了多个公共播放列表,但是它们都产生了一个空的list对象。该代码大约在3周前生效。另外,我正在运行Python 3.7.6和最新版本的PyTube3(9.6.4)。

我在做错什么吗?

1 个答案:

答案 0 :(得分:6)

我对源进行了一些调查,看来它将加载html,然后对/watch/v=... URL执行正则表达式搜索。使用的正则表达式为href=\"(/watch\?v=[\w-]*),但由于YouTube必须更新其html,因此找不到任何匹配项。现在,他们在JSON对象中发送监视URL。所以我们应该寻找它。

这是可行的方法:

from pytube import Playlist
import re

playlist = Playlist("https://www.youtube.com/playlist?list=PLynhp4cZEpTbRs_PYISQ8v_uwO0_mDg_X")
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")

print(len(playlist.video_urls))
for url in playlist.video_urls:
    print(url)

希望这对下一个补丁有用。