在YouTube上搜索并返回Python中的所有链接

在YouTube上搜索并返回Python中的所有链接,python,selenium,youtube,webdriver,webdriverwait,Python,Selenium,Youtube,Webdriver,Webdriverwait,在YouTube上,我想搜索某些视频(即Python上的视频),然后,我想返回此搜索返回的所有视频。现在,如果我尝试使用此Python,它将返回起始页上的所有视频,而不是搜索后的页面上的所有视频 当前代码: from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install()

在YouTube上,我想搜索某些视频(即Python上的视频),然后,我想返回此搜索返回的所有视频。现在,如果我尝试使用此Python,它将返回起始页上的所有视频,而不是搜索后的页面上的所有视频

当前代码:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://youtube.com")
driver.find_element_by_name("search_query").send_keys("Python")
driver.find_element_by_id("search-icon-legacy").click()
links = driver.find_elements_by_id("video-title")
for x in links:
    print(x.get_attribute("href"))

这里出了什么问题?

但最好使用显式等待:

links = ui.WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.ID, "video-title")))


希望它能帮助你

根据与@Mark的讨论:

Youtube首页的元素似乎仍在DOM中

我看到的唯一修复方法是转到搜索URL:

driver.get("http://youtube.com/results?search_query=Python")
# driver.find_element_by_name("search_query").send_keys("Python")
# driver.find_element_by_id("search-icon-legacy").click()

您应该使用WebDriverWait而不是sleep:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from selenium.webdriver.chrome.options import Options

opt = Options()
opt.add_argument("--incognito")

driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=opt)
driver.get("http://youtube.com")
driver.find_element_by_name("search_query").send_keys("Python")
driver.find_element_by_id("search-icon-legacy").click()
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.ID, "video-title")))
links = driver.find_elements_by_id("video-title")

for x in links:

    print(x.get_attribute("href"))
输出:

https://www.youtube.com/watch?v=rfscVS0vtbw
https://www.youtube.com/watch?v=f79MRyMsjrQ
https://www.youtube.com/watch?v=kLZuut1fYzQ
https://www.youtube.com/watch?v=N4mEzFDjqtA
https://www.youtube.com/watch?v=Z1Yd7upQsXY
https://www.youtube.com/watch?v=hnDU1G9hWqU
https://www.youtube.com/watch?v=3cZsjOclmoM
https://www.youtube.com/watch?v=f3EbDbm8XqY
https://www.youtube.com/watch?v=2uCXIbkbDSE
https://www.youtube.com/watch?v=HXV3zeQKqGY
https://www.youtube.com/watch?v=JJmcL1N2KQs
https://www.youtube.com/watch?v=qiSCMNBIP2g
https://www.youtube.com/watch?v=7lmCu8wz8ro
https://www.youtube.com/watch?v=25ovCm9jKfA
https://www.youtube.com/watch?v=q6Mc_sAPZ2Y
https://www.youtube.com/watch?v=yE9v9rt6ziw
https://www.youtube.com/watch?v=Y8Tko2YC5hA
https://www.youtube.com/watch?v=G0rQ7AEl5LA
https://www.youtube.com/watch?v=CtbckFw0pJs
https://www.youtube.com/watch?v=sugvnHA7ElY

要从搜索中返回关键字为Python的所有视频,您需要:

  • 最大化屏幕,使所有生成的视频链接都在屏幕内呈现
  • 在提取href属性之前,诱导WebDriverWait使所需元素可见
  • 您可以使用以下解决方案

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      options = webdriver.ChromeOptions()
      options.add_argument("start-maximized")
      options.add_argument("disable-infobars")
      options.add_argument("--disable-extensions")
      driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("https://www.youtube.com/")
      WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#search"))).send_keys("Python")
      driver.find_element_by_css_selector("button.style-scope.ytd-searchbox#search-icon-legacy").click()
      print([my_href.get_attribute("href") for my_href in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.yt-simple-endpoint.style-scope.ytd-video-renderer#video-title")))])
      
    • 控制台输出:

      ['https://www.youtube.com/watch?v=rfscVS0vtbw', 'https://www.youtube.com/watch?v=7UeRnuGo-pg', 'https://www.youtube.com/watch?v=3cZsjOclmoM', 'https://www.youtube.com/watch?v=f79MRyMsjrQ', 'https://www.youtube.com/watch?v=CtbckFw0pJs', 'https://www.youtube.com/watch?v=Z1Yd7upQsXY', 'https://www.youtube.com/watch?v=kLZuut1fYzQ', 'https://www.youtube.com/watch?v=IZ0IM_T4aio', 'https://www.youtube.com/watch?v=qiSCMNBIP2g', 'https://www.youtube.com/watch?v=N0lxfilGfak', 'https://www.youtube.com/watch?v=N4mEzFDjqtA', 'https://www.youtube.com/watch?v=s3Ejdx6cIho', 'https://www.youtube.com/watch?v=Y8Tko2YC5hA', 'https://www.youtube.com/watch?v=c3FXQU3TyCU', 'https://www.youtube.com/watch?v=yE9v9rt6ziw', 'https://www.youtube.com/watch?v=yvHrNlAF0Y0', 'https://www.youtube.com/watch?v=ZDa-Z5JzLYM']
      

错误是什么?您的代码看起来很好。错误是什么?它只在搜索结果之后返回,对吗?除此之外,你还想返回什么?我是说你的问题到底是什么?你们想做什么?伙计们,他已经清楚地提到了要求,答案已经到了。请重新阅读问题。但输出中的第一个链接不是您在youtube上搜索python视频时获得的第一个视频。。?这就是我想要的want@MarkWekking你在找广告URL吗?不,我想要你在youtube上搜索Python时得到的视频链接列表,理论上第一个链接应该是:。因为当你搜索Python视频时,这是它返回的第一个视频()@MarkWekking,我发现了问题。。。您需要将
--incognito
添加到
chrome\u选项中
我会将其添加到我的答案中…:)这并不能解决我的问题。。我仍然会看到youtube主屏幕上的视频,而不是搜索词之后的视频。浏览器与此有什么关系?