born
This commit is contained in:
75
autorsearcher.py
Normal file
75
autorsearcher.py
Normal file
@ -0,0 +1,75 @@
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.chrome.options import Options
|
||||
from bs4 import BeautifulSoup
|
||||
import requests
|
||||
|
||||
def get_html_without_javascript(url):
|
||||
"""
|
||||
Utiliza Selenium para obtener el HTML de una página web con JavaScript desactivado.
|
||||
"""
|
||||
# Configuración de opciones de Chrome
|
||||
chrome_options = Options()
|
||||
chrome_options.add_argument("--disable-dev-shm-usage") # Usa memoria compartida si no hay suficiente RAM
|
||||
chrome_options.add_argument("--no-sandbox") # Desactiva el sandboxing (problema común en entornos sin root)
|
||||
chrome_options.add_argument("--disable-extensions") # Desactiva extensiones del navegador
|
||||
chrome_options.add_argument("--remote-debugging-port=9222") # Debugging remoto
|
||||
chrome_options.add_argument("--headless=new") # Nueva implementación de headless
|
||||
|
||||
|
||||
# Inicializar el driver de Chrome
|
||||
driver = webdriver.Chrome(options=chrome_options)
|
||||
|
||||
try:
|
||||
# Accede al sitio web
|
||||
driver.get(url)
|
||||
# Obtener el contenido de la página
|
||||
html_content = driver.page_source
|
||||
return html_content
|
||||
finally:
|
||||
# Cierra el navegador
|
||||
driver.quit()
|
||||
|
||||
def get_author_from_html(html_content):
|
||||
"""
|
||||
Utiliza BeautifulSoup para extraer el autor de una página HTML.
|
||||
"""
|
||||
try:
|
||||
# Analizar el HTML con BeautifulSoup
|
||||
soup = BeautifulSoup(html_content, 'html.parser')
|
||||
|
||||
# Buscar la meta tag con name="autor"
|
||||
author_meta = soup.find('meta', attrs={'name': 'autor'})
|
||||
|
||||
# Si existe, devolver el contenido del atributo "content"
|
||||
if author_meta and 'content' in author_meta.attrs:
|
||||
return author_meta['content']
|
||||
else:
|
||||
return "No se encontró el autor en la meta etiqueta."
|
||||
except Exception as e:
|
||||
return f"Error al analizar el HTML: {e}"
|
||||
|
||||
# Función principal
|
||||
def main(url):
|
||||
"""
|
||||
Combina ambas funciones para obtener el autor desde una página con JS desactivado.
|
||||
"""
|
||||
try:
|
||||
# Obtener el HTML sin JavaScript usando Selenium
|
||||
print("Obteniendo HTML con JavaScript desactivado...")
|
||||
html_content = get_html_without_javascript(url)
|
||||
|
||||
# Guardar el HTML en un archivo local (opcional)
|
||||
with open("pagina_web_sin_js.html", "w", encoding="utf-8") as file:
|
||||
file.write(html_content)
|
||||
|
||||
# Obtener el autor desde el HTML
|
||||
print("Analizando HTML para obtener el autor...")
|
||||
author = get_author_from_html(html_content)
|
||||
return author
|
||||
except Exception as e:
|
||||
return f"Error general: {e}"
|
||||
|
||||
# Ejemplo de uso
|
||||
url = "https://www.abc.es/internacional/trump-presidente-vez-era-dorada-empieza-momento-20250120202851-nt.html"
|
||||
author = main(url)
|
||||
print(f"El autor es: {author}")
|
Reference in New Issue
Block a user