首页 > 解决方案 > 在 YouTube 上搜索并返回 n 个链接

问题描述

我正在尝试从 youtube 下载某些给定关键字的视频。现在我正在使用这个代码 -

import urllib.request
import re

search_keyword="race+car"
html = urllib.request.urlopen("https://www.youtube.com/results?search_query=" + search_keyword)
video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())
for i in range(0,len(video_ids)):
  #print("https://www.youtube.com/watch?v=" + video_ids[i])
  youtube_video_url = "https://www.youtube.com/watch?v=" + video_ids[i]

  try:
    yt_obj = YouTube(youtube_video_url)
    filters = yt_obj.streams.filter(progressive=True, file_extension='mp4')
    filters.get_highest_resolution().download()

    print('Video Downloaded Successfully')
  except Exception as e:
    print(e)

print(len(video_ids))

这现在为我下载了 30 个视频。有没有办法下载我想要的任意数量的视频,比如 100 个或 1000 个?

标签: pythonweb-scrapingyoutube

解决方案


推荐阅读