Estoy tratando de hacer un corpus de comentarios en cierto video de youtube con selenium y BeautifulSoup. (No estoy tratando de usar la API de datos de Youtube, debido al límite).
y casi lo hago, pero podría haber obtenido el resultado con solo comentarios e identificadores ...
Revisé el espacio que contiene la información de recuentos similares y luego lo ingresé en mi código, de todos modos va bien, pero no recupera el resultado, no me da nada ... no sé por qué ... ..
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd
import re
from collections import Counter
from konlpy.tag import Twitter
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(executable_path='C:\chrome\chromedriver_win32\chromedriver.exe', options=options)
url = 'https://www.youtube.com/watch?v=D4pxIxGdR_M&t=2s'
driver.get(url)
driver.implicitly_wait(10)
SCROLL_PAUSE_TIME = 3
# Get scroll height
last_height = driver.execute_script("return document.documentElement.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.documentElement.scrollHeight")
if new_height == last_height:
break
last_height = new_height
html_source = driver.page_source
driver.close()
soup = BeautifulSoup(html_source, 'lxml')
ids = soup.select('div#header-author > a > span')
comments = soup.select('div#content > yt-formatted-string#content-text')
likes = soup.select('ytd-comment-action-buttons-renderer#action-buttos > div#tollbar > span#vote-count-middle')
print('ID :', len(ids), 'Comments : ', len(comments), 'Likes : ' ,len(likes))
y 0 se acaba de imprimir ... he buscado algunas de las formas de lidiar con eso, pero la mayoría de las respuestas fueron solo para hacerme usar la API.
En realidad, no usaría BeautifulSoup para la extracción, solo vaya con las herramientas de selenio integradas, es decir:
ids = driver.find_elements_by_xpath('//*[@id="author-text"]/span')
comments = driver.find_elements_by_xpath('//*[@id="content-text"]')
likes = driver.find_elements_by_xpath('//*[@id="vote-count-middle"]')
De esta manera, aún puede usarlos len()
debido a que son iterables. También puede iterar sobre la variable likes
y obtener el .text
valor para sumarlos:
total_likes = 0
for like in likes:
total_likes += int(like.text)
Para obtener esto más pitónico, también podría ir con una lista de comprensión adecuada.
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras