我想搜索特定的关键字,然后抓取所有视频网址。
我知道我要粘贴的代码不会这样做,但我想展示我所做的事情。
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"))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)