gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:08:54 24 4
gpt4 key购买 nike

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

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

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"))

关于python - 如何从 YouTube 搜索中抓取视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45362731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com