如何使用 pytube 快速获取 YouTube 播放列表中的视频标题?

How can I quickly get titles of videos in YouTube playlist using pytube?

我需要获取 1,一个播放列表的视频列表 link 和 2,一个播放列表的视频名称列表。这就是我正在做的。

from pytube import YouTube, Playlist

playlist_link = "https://www.youtube.com/playlist?list=PLJKfZ_cKGyLdYqdzGLCJPbsi9UGCcEc5e"

video_links = Playlist(playlist_link).video_urls

video_titles = []
for link in video_links:
    video_titles.append(YouTube(link).title)

虽然这可行,但获取所有标题需要很长时间,因为每个 link 都必须转换为 YouTube object,有没有更快的方法来做到这一点?

我看了你的代码,执行大约需要 22 秒。

from time import time
from pytube import YouTube, Playlist

playlist_link = "https://www.youtube.com/playlist?list=PLJKfZ_cKGyLdYqdzGLCJPbsi9UGCcEc5e"

video_links = Playlist(playlist_link).video_urls

video_titles = []
start = time()
for link in video_links:
    video_titles.append(YouTube(link).title)

print(f'Time taken: {time() - start}')
# output
Time taken: 21.815414667129517

您可以通过添加多线程来减少执行时间。以下代码执行大约需要 3 秒。

from time import time
from pytube import YouTube, Playlist
from concurrent.futures import ThreadPoolExecutor, as_completed

playlist_link = "https://www.youtube.com/playlist?list=PLJKfZ_cKGyLdYqdzGLCJPbsi9UGCcEc5e"
video_links = Playlist(playlist_link).video_urls
start = time()


def get_video_title(link):
    title = YouTube(link).title
    return title


processes = []
with ThreadPoolExecutor(max_workers=10) as executor:
    for url in video_links:
        processes.append(executor.submit(get_video_title, url))

video_titles = []
for task in as_completed(processes):
    video_titles.append(task.result())
    print(task.result())


print(f'Time taken: {time() - start}')
# output
Time taken: 2.7463150024414062