noob我正在看这个python selenium网页抓取教程/watch?v= lTypMlVBFM 4我不知道为什么它不能从youtube上获取标题并打印出来

63lcw9qa  于 2023-01-30  发布在  Python
关注(0)|答案(1)|浏览(122)

我正试图废弃这个网站的视频标题https://www.youtube.com/@JohnWatsonRooney/videos
这个简短的解释并没有帮助与代码的问题,但我想我会说,无论如何。我这样做,所以我可以得到我的脚在门上,然后废弃其他网站的东西的价格,所以我可以做一个程序,给我一个链接到最便宜的价格产品从名单几个网站以及所有其他产品的排名,并有名称的情况下,这是'这就是我要找的人。
这是终端输出。

Traceback (most recent call last):
File "C:\\Users\\PRO\\Documents\\dynamic_web_scraping_test.py", line 11, in \<module\>
videos = driver.find_element("name", "style-scope ytd-rich-grid-media")
File "C:\\Users\\PRO\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\selenium\\webdriver\\remote\\webdriver.py", line 830, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})\["value"\]
File "C:\\Users\\PRO\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\selenium\\webdriver\\remote\\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\\Users\\PRO\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: \[name="style-scope ytd-rich-grid-media"\]
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:180:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:392:5
element.find/\</\<@chrome://remote/content/marionette/element.sys.mjs:275:16

C:\\Users\\PRO\\Documents\>

这是我代码

import time
from selenium import webdriver
url1 = 'https://www.youtube.com/@JohnWatsonRooney/videos'
url2 = 'https://www.youtube.com/@MyCraftyDen/videos'
driver = webdriver.Firefox()
driver.get(url1)
time.sleep(10)
videos = driver.find_element("name", "style-scope ytd-rich-grid-media")
for video in videos:
titles = video.find_element_by_xpath('//\*\[@id="video-title-link"\]').text
print(title)

我不知道该怎么做,我在发帖前尽了最大的努力查找这个问题。我知道等待浏览器加载页面不会起作用。在我的代码中使用sleep(10)。

gcmastyq

gcmastyq1#

如果你想找到给定页面中所有视频的链接,你可以使用下面的代码

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

url1 = 'https://www.youtube.com/@JohnWatsonRooney/videos'
url2 = 'https://www.youtube.com/@MyCraftyDen/videos'
driver = webdriver.Firefox()
driver.get(url1)
time.sleep(10)
videos = driver.find_elements(By.ID, "video-title-link")
for video in videos:
    title = video.get_attribute("href")
    print(title)

相关问题