Herramientas de usuario

Herramientas del sitio


ia:desinformadores

¡Esta es una revisión vieja del documento!


<!---
MIT License
 
Copyright (c) 2025 wiki.acosadores.net
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
 
def obtener_videos_con_scroll(url_canal):
    # Inicializa el navegador (asegúrate de tener el driver de Chrome o Firefox)
    driver = webdriver.Chrome()  # O webdriver.Firefox(), dependiendo de tu elección
 
    # Navega a la página del canal
    driver.get(url_canal)
    time.sleep(5)  # Espera un poco más para que la página cargue completamente
 
    # Simula el scroll hacia abajo para cargar más videos
    SCROLL_PAUSE_TIME = 4  # Aumenta el tiempo de pausa
    last_height = driver.execute_script("return document.documentElement.scrollHeight")
 
    videos_cargados = set()  # Usamos un conjunto para evitar duplicados
 
    while True:
        # Desplaza hacia abajo
        driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
        time.sleep(SCROLL_PAUSE_TIME)
 
        # Calcula la nueva altura de la página y compara con la anterior
        new_height = driver.execute_script("return document.documentElement.scrollHeight")
 
        # Recoge los videos visibles usando el nuevo selector
        videos = driver.find_elements(By.XPATH, '//ytd-rich-grid-media//yt-formatted-string[@id="video-title"]')
 
        # Depurar: Imprimir cuántos videos se encontraron en esta iteración
        print(f"Videos encontrados en esta iteración: {len(videos)}")
 
        for video in videos:
            title = video.text  # Usar .text para obtener el título visible
            link = video.find_element(By.XPATH, './ancestor::ytd-rich-grid-media//a').get_attribute('href')  # Obtener el enlace
            # Añade a un conjunto para evitar duplicados
            videos_cargados.add((title, link))
 
        # Si la altura no cambia, hemos llegado al final
        if new_height == last_height:
            break
 
        last_height = new_height
 
    driver.quit()
 
    # Convertir el conjunto a lista de diccionarios
    lista_videos = [{'titulo': title, 'link': link} for title, link in videos_cargados]
 
    # Depurar: imprimir la cantidad total de videos recogidos
    print(f"Total de videos recogidos: {len(lista_videos)}")
 
    return lista_videos
 
def obtener_shorts_con_scroll(url_canal):
    # Inicializa el navegador (asegúrate de tener el driver de Chrome o Firefox)
    driver = webdriver.Chrome()  # O webdriver.Firefox(), dependiendo de tu elección
 
    # Navega a la página de Shorts del canal
    driver.get(url_canal)
    time.sleep(5)  # Espera un poco para que la página cargue completamente
 
    # Simula el scroll hacia abajo para cargar más shorts
    SCROLL_PAUSE_TIME = 3
    last_height = driver.execute_script("return document.documentElement.scrollHeight")
 
    shorts_cargados = set()  # Usamos un conjunto para evitar duplicados
 
    while True:
        # Desplaza hacia abajo
        driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
        time.sleep(SCROLL_PAUSE_TIME)
 
        # Esperar explícitamente hasta que los elementos de shorts estén presentes
        shorts = WebDriverWait(driver, 10).until(
            EC.presence_of_all_elements_located((By.XPATH, '//a[contains(@class, "shortsLockupViewModelHostEndpoint")]'))
        )
 
        # Depurar: Imprimir cuántos shorts se encontraron en esta iteración
        print(f"Shorts encontrados en esta iteración: {len(shorts)}")
 
        for short in shorts:
            try:
                # Extraer título y enlace de cada short
                title = short.get_attribute('title')
                link = "https://www.youtube.com" + short.get_attribute('href')
                # Añadir al conjunto
                shorts_cargados.add((title, link))
            except Exception as e:
                print(f"Error al extraer un short: {e}")
 
        # Calcular la nueva altura y compararla con la anterior
        new_height = driver.execute_script("return document.documentElement.scrollHeight")
        if new_height == last_height:
            break
        last_height = new_height
 
    driver.quit()
 
    # Convertir el conjunto a lista de diccionarios
    lista_shorts = [{'titulo': title, 'link': link} for title, link in shorts_cargados]
 
    # Depurar: imprimir la cantidad total de shorts recogidos
    print(f"Total de shorts recogidos: {len(lista_shorts)}")
 
    return lista_shorts
 
 
 
 
def obtener_streams_con_scroll(url_canal):
    # Inicializa el navegador (asegúrate de tener el driver de Chrome o Firefox)
    driver = webdriver.Chrome()  # O webdriver.Firefox(), dependiendo de tu elección
 
    # Navega a la página de Streams del canal
    driver.get(url_canal)
    time.sleep(4)  # Espera un poco más para que la página cargue completamente
 
    # Simula el scroll hacia abajo para cargar más streams
    SCROLL_PAUSE_TIME = 3  # Aumenta el tiempo de pausa
    last_height = driver.execute_script("return document.documentElement.scrollHeight")
 
    streams_cargados = set()  # Usamos un conjunto para evitar duplicados
 
    while True:
        # Desplaza hacia abajo
        driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
        time.sleep(SCROLL_PAUSE_TIME)
 
        # Calcula la nueva altura de la página y compara con la anterior
        new_height = driver.execute_script("return document.documentElement.scrollHeight")
 
        # Recoge los streams visibles usando el nuevo selector
        streams = driver.find_elements(By.XPATH, '//a[@id="video-title-link"]')
 
        # Depurar: Imprimir cuántos streams se encontraron en esta iteración
        print(f"Streams encontrados en esta iteración: {len(streams)}")
 
        for stream in streams:
            title = stream.get_attribute('title')  # Obtener el título
            link = "https://www.youtube.com" + stream.get_attribute('href')  # Obtener el enlace completo
            # Añade a un conjunto para evitar duplicados
            streams_cargados.add((title, link))
 
        # Si la altura no cambia, hemos llegado al final
        if new_height == last_height:
            break
 
        last_height = new_height
 
    driver.quit()
 
    # Convertir el conjunto a lista de diccionarios
    lista_streams = [{'titulo': title, 'link': link} for title, link in streams_cargados]
 
    # Depurar: imprimir la cantidad total de streams recogidos
    print(f"Total de streams recogidos: {len(lista_streams)}")
 
    return lista_streams
 
# URL del canal (videos, shorts, streams)
url_videos = "https://www.youtube.com/@geoestratego_oficial/videos"
url_shorts = "https://www.youtube.com/@geoestratego_oficial/shorts"
url_streams = "https://www.youtube.com/@geoestratego_oficial/streams"
 
# Obtener videos, shorts y streams
titulos_shorts = obtener_shorts_con_scroll(url_shorts)
titulos_videos = obtener_videos_con_scroll(url_videos)
titulos_streams = obtener_streams_con_scroll(url_streams)
 
# Combinar resultados
titulos_totales = titulos_videos + titulos_shorts + titulos_streams
 
# Imprimir los títulos y enlaces de los videos, shorts y streams
for titulo in titulos_totales:
    print(f"Título: {titulo['titulo']}, Enlace: {titulo['link']}")
 
python3 raspado.py > geoestratego.txt
cat geoestratego.txt | grep Título | sed '/^Título: ,/d;s/^Título: //g;s/, Enlace: .*$//g;s/[A-Z]/\L&/g;y/áéíóú/aeiou/;s/[^a-zñ ]//g;s/ \{2,\}/ /g'
ia/desinformadores.1737868059.txt.gz · Última modificación: 2025/01/26 06:07 por anonimo