删除YouTube评论中的所有回复

时间:2018-12-12 16:09:45

标签: python selenium screen-scraping

我正在尝试从Youtube中提取对所有评论的所有回复。
我可以抓取所有评论,但无法获得其下的回复。我有这个错误。我在Linux上。

Message: Element <paper-button id="more" class="style-scope ytd-expander"> could not be scrolled into view

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains

driver=webdriver.Firefox()
driver.get('https://www.youtube.com/watch?v=QOzZDdXetUA')
driver.execute_script('window.scrollTo(1, 500);')

time.sleep(5)
driver.execute_script('window.scrollTo(1, 3000);')

comments=driver.find_elements_by_xpath('//*[@id="content-text"]')
name=driver.find_elements_by_xpath('//*[@id="author-text"]')

while True:
    try:
        loadMoreButton = driver.find_element_by_xpath('//*[@id="more"]')
        time.sleep(2)
        loadMoreButton.click()
        time.sleep(5)
    except Exception as e:
        print(e)
        break

time.sleep(10)

number_of_items=len(name)
for i in range (number_of_items):
    print(name[i].text + " : ")
    print(comments[i].text)
driver.quit()   

1 个答案:

答案 0 :(得分:0)

好吧,我认为您很封闭。我在您已有的内容中添加了几行。这是您想要的吗?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True

driver=webdriver.Firefox(executable_path="C:/Utility/geckodriver.exe", firefox_profile=profile)
driver.get('https://www.youtube.com/watch?v=QOzZDdXetUA')
driver.execute_script('window.scrollTo(1, 500);')

time.sleep(5)
driver.execute_script('window.scrollTo(1, 3000);')

comments=driver.find_elements_by_xpath('//*[@id="content-text"]')
name=driver.find_elements_by_xpath('//*[@id="author-text"]')

while True:
    try:
        loadMoreButton = driver.find_element_by_xpath('//*[@id="more"]')
        time.sleep(2)
        loadMoreButton.click()
        time.sleep(5)
    except Exception as e:
        print(e)
        break

time.sleep(10)

number_of_items=len(name)
for i in range (number_of_items):
    print(name[i].text + " : ")
    print(comments[i].text)
driver.quit()