【发布时间】:2018-03-10 04:10:54
【问题描述】:
我正在尝试使用 selenium 获取 youtube cmets,但出现错误。任何人都可以帮我解决这个问题。
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.get("https://www.youtube.com/watch?v=MNltVQqJhRE")
time.sleep(10)
D=driver.find_element_by_xpath('//yt-formatted-string[@id="content-text"]')
print(D.text)
我明白了
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//yt-formatted-string[@id="content-text"]"}
它适用于这段代码。但它没有抓取所有的 cmets。我在堆栈溢出中找到了这段代码。
driver=webdriver.Chrome()
driver.get('https://www.youtube.com/watch?v=iFPMz36std4')
driver.execute_script('window.scrollTo(1, 500);')
#now wait let load the comments
time.sleep(5)
driver.execute_script('window.scrollTo(1, 3000);')
comment_div=driver.find_element_by_xpath('//*[@id="contents"]')
comments=comment_div.find_elements_by_xpath('//*[@id="content-text"]')
for comment in comments:
print(comment.text)
【问题讨论】:
标签: python python-3.x selenium selenium-chromedriver