Как извлечь количество комментариев к видео на YouTube с помощью Google Chrome Headless и Selenium? - PullRequest
1 голос
/ 12 июля 2020

На каждой веб-странице YouTube есть элемент, показывающий, сколько комментариев к видео. Это такая структура html:

<yt-formatted-string class="count-text style-scope ytd-comments-header-renderer">xx Comments</yt-formatted-string>

Я хочу получить число xx Comments с селеном. code1-с головным браузером

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
options = webdriver.ChromeOptions()
proxy = '127.0.0.1:1080'   
options.add_argument('--proxy-server=socks5://' + proxy)
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver,30)
url='https://www.youtube.com/watch?v=N0lxfilGfak'

driver.get(url)
driver.execute_script("return scrollBy(0, 1000);")
comment = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//yt-formatted-string[contains(., 'Comments')]")))
driver.execute_script("arguments[0].scrollIntoView(true);",comment)
print(driver.find_element_by_xpath("//h2[@id='count']").text)

С указанным выше кодом python я могу получить 717 Comments для https://www.youtube.com/watch?v=N0lxfilGfak.

Теперь я хочу получить тот же номер с безголовым браузером в селене. code2-с безголовым браузером.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
options = webdriver.ChromeOptions()
proxy = '127.0.0.1:1080'   
options.add_argument('--proxy-server=socks5://' + proxy)
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--headless")
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver,30)
url='https://www.youtube.com/watch?v=N0lxfilGfak'

driver.get(url)
driver.execute_script("return scrollBy(0, 1000);")
comment = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//yt-formatted-string[contains(., 'Comments')]")))
driver.execute_script("arguments[0].scrollIntoView(true);",comment)
print(driver.find_element_by_xpath("//h2[@id='count']").text)

Примечание: в code2 на три строки больше, чем в code1.

options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--headless")

Остальные строки одинаковы как в code2, так и в code1.

Он застревает в операторе comment при выполнении code2:

>>> comment = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//yt-formatted-string[contains(., 'Comments')]")))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

Почему не удается получить элемент с помощью безголового браузера в селене?

Ответы [ 2 ]

1 голос
/ 12 июля 2020

Вы были почти у цели. Чтобы напечатать текст xx Комментарии с использованием Selenium под управлением ChromeDriver инициирован Контекст просмотра вы должны вызвать WebDriverWait для visibility_of_element_located(), и вы можете использовать любую из следующих Стратегий локатора :

  • Использование XPATH и текста атрибут:

    driver.get("https://www.youtube.com/watch?v=N0lxfilGfak")
    driver.execute_script("return scrollBy(0, 1000);")
    subscribe = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//yt-formatted-string[text()='Subscribe']")))
    driver.execute_script("arguments[0].scrollIntoView(true);",subscribe)
    print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,"//h2[@id='count']/yt-formatted-string"))).text)
    
  • Использование CSS_SELECTOR и get_attribute():

    driver.get("https://www.youtube.com/watch?v=N0lxfilGfak")
    driver.execute_script("return scrollBy(0, 1000);")
    subscribe = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//yt-formatted-string[text()='Subscribe']")))
    driver.execute_script("arguments[0].scrollIntoView(true);",subscribe)
    print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"h2#count>yt-formatted-string"))).get_attribute("innerHTML"))
    
  • Вывод в консоль:

    717 Comments
    
  • Примечание : вам необходимо добавить следующие импорты:

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

Использование Headless Chrome

Используя , вы можете использовать следующее решение:

  • Блок кода:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument('--headless')
    options.add_argument('--window-size=1920,1080')
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.youtube.com/watch?v=N0lxfilGfak")
    driver.execute_script("return scrollBy(0, 1000);")
    subscribe = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//yt-formatted-string[text()='Subscribe']")))
    driver.execute_script("arguments[0].scrollIntoView(true);",subscribe)
    # using xpath and text attribute
    print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,"//h2[@id='count']/yt-formatted-string"))).text)
    # using cssSelector and get_attribute()
    print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"h2#count>yt-formatted-string"))).get_attribute("innerHTML"))
    print("Exiting")
    driver.quit()
    
  • Вывод на консоль:

    717 Comments
    717 Comments
    Exiting
    
0 голосов
/ 12 июля 2020

Добавить строку в мои настройки для headless настройки:

options.add_argument('--window-size=1920,1080')

Или увеличить длину прокрутки по оси Y.

driver.execute_script("return scrollBy(0, 5000);")

Мое выражение xpath более прямое.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...