python - 如何从 YouTube 搜索中抓取视频?

我想搜索特定关键字,然后抓取所有视频网址。

我知道我要粘贴的代码不会这样做,但我想展示我所做的。

chrome_path = r"C:\Users\Admin\Documents\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.youtube.com/results?sp=CAISAggBUBQ%253D&q=minecraft")

links = driver.find_elements_by_partial_link_text('/watch')
for link in links:
    links = (links.get_attribute("href"))

如何抓取链接并将它们保存到文件中?

最佳答案

这是您的代码 为您提供视频的标题和网址 轻巧简单:)

from bs4 import BeautifulSoup
import urllib.request


def SearchVid(search):
    responce = urllib.request.urlopen('https://www.youtube.com/results?search_query='+search)

    soup = BeautifulSoup(responce)    
    divs = soup.find_all("div", { "class" : "yt-lockup-content"})


    for i in divs:
        href= i.find('a', href=True)
        print(href.text,  "\nhttps://www.youtube.com"+href['href'], '\n')
        with open(SearchString.replace("%20", "_")+'.txt', 'a') as writer:
            writer.write("https://www.youtube.com"+href['href']+'\n')

print("What are you looking for?")
SearchString = input()
SearchVid(SearchString.replace(" ", "%20"))

https://stackoverflow.com/questions/45362731/

相关文章:

php - 使用 PHP curl 从 Google 的地理编码 API 返回数据

laravel - smtp 中继 - g 套件 - 带有自定义域电子邮件的 gmail

android - setMultiChoiceItems 对话框中的图标 - Android

bash - 如何在 Powerline-Plain [Bash-it] 主题的提示中显示彩色主机名

outlook - 仅在 Web 上启用 Outlook Web 插件

python - 管道输入错误 "Input Contains NaN"

ios - Xcode 和安装的 SF Pro Display(Apple 字体)

java - Hibernate ManyToOne 保存对象

sql - 使用 SQL 查询将 Excel 数据加载到 Azure SQL 中

hibernate - 如何将表名作为参数传递给 HQL 查询