Add initial implementation of Chrome user profile management and scraping driver
This commit is contained in:
@@ -0,0 +1,51 @@
|
|||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
def crear_carpeta_usuario(ruta_chrome, ruta_carpeta_usuario):
|
||||||
|
# Asegúrate de que la ruta proporcionada para la carpeta del usuario existe
|
||||||
|
if not os.path.exists(ruta_carpeta_usuario):
|
||||||
|
os.makedirs(ruta_carpeta_usuario)
|
||||||
|
|
||||||
|
# Comando para iniciar Chrome con la carpeta de usuario personalizada
|
||||||
|
comando = [
|
||||||
|
ruta_chrome, # Ruta del ejecutable de Chrome
|
||||||
|
f"--user-data-dir={ruta_carpeta_usuario}" # Carpeta de usuario personalizada
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Ejecuta Chrome con el perfil de usuario personalizado
|
||||||
|
subprocess.run(comando, check=True)
|
||||||
|
print(f"Chrome iniciado con la carpeta de usuario: {ruta_carpeta_usuario}")
|
||||||
|
except FileNotFoundError:
|
||||||
|
print("El ejecutable de Chrome no se encontró. Verifica la ruta proporcionada.")
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print(f"Hubo un error al iniciar Chrome: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def obtener_version_chrome(ruta_chrome):
|
||||||
|
try:
|
||||||
|
# Ejecuta el comando para obtener la versión de Chrome
|
||||||
|
resultado = subprocess.run([ruta_chrome, "--version"], capture_output=True, text=True, check=True)
|
||||||
|
# La salida del comando incluye la versión de Chrome
|
||||||
|
version = resultado.stdout.strip()
|
||||||
|
print(version)
|
||||||
|
return version
|
||||||
|
except FileNotFoundError:
|
||||||
|
print("El ejecutable de Chrome no se encontró. Verifica la ruta proporcionada.")
|
||||||
|
return None
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print(f"Hubo un error al obtener la versión de Chrome: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# Ejemplo de uso
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# ruta_chrome = input("Introduce la ruta completa al ejecutable de Chrome: ")
|
||||||
|
# ruta_carpeta_usuario = input("Introduce la ruta donde deseas crear la carpeta de usuario: ")
|
||||||
|
nombre_usuario = "usuario1"
|
||||||
|
|
||||||
|
|
||||||
|
ruta_chrome = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
|
||||||
|
ruta_carpeta_usuario = rf"E:\Proyects\Herramientas_para_scrapping\perfiles_chrome\{nombre_usuario}"
|
||||||
|
|
||||||
|
crear_carpeta_usuario(ruta_chrome, ruta_carpeta_usuario)
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
import undetected_chromedriver as uc
|
||||||
|
|
||||||
|
|
||||||
|
def driver_scrapping(carpeta_descargas, carpeta_perfil_chrome, ejecutable_chrome, headless=False):
|
||||||
|
"""
|
||||||
|
Description:
|
||||||
|
Initializes a Chrome session with specific options and settings based on provided parameters.
|
||||||
|
Configures download folder, user profile directory, and Chrome executable.
|
||||||
|
Sets the browser window size and returns the configured driver object for external use.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
carpeta_descargas (str): Path to the download directory.
|
||||||
|
carpeta_perfil_chrome (str): Path to the Chrome user profile directory.
|
||||||
|
ejecutable_chrome (str): Path to the Chrome executable.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
driver (WebDriver): Configured WebDriver object ready for automation tasks.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
driver = driver_scrapping(download_folder, profile_folder, executable_path)
|
||||||
|
|
||||||
|
Created_at: 2024-05-04
|
||||||
|
Updated_at: 2024-05-04
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Configuration settings for the Chrome browser
|
||||||
|
options = uc.ChromeOptions()
|
||||||
|
options.headless = headless # Set to True for headless operation
|
||||||
|
options.add_argument("--disable-blink-features=AutomationControlled")
|
||||||
|
options.add_argument("--password-store=basic")
|
||||||
|
|
||||||
|
# Add experimental options and preferences for browser configuration
|
||||||
|
options.add_experimental_option(
|
||||||
|
"prefs",
|
||||||
|
{
|
||||||
|
"download.default_directory": carpeta_descargas,
|
||||||
|
"download.prompt_for_download": False,
|
||||||
|
"download.directory_upgrade": True,
|
||||||
|
"safebrowsing.enabled": True,
|
||||||
|
"credentials_enable_service": False,
|
||||||
|
"profile.password_manager_enabled": False,
|
||||||
|
"plugins.always_open_pdf_externally": True,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Initialize the Chrome driver with specified options
|
||||||
|
driver = uc.Chrome(options=options,
|
||||||
|
user_data_dir=carpeta_perfil_chrome,
|
||||||
|
browser_executable_path=ejecutable_chrome)
|
||||||
|
|
||||||
|
# Set browser window size
|
||||||
|
driver.set_window_size(1200, 800)
|
||||||
|
|
||||||
|
print("Browser configured and ready for use.")
|
||||||
|
|
||||||
|
return driver
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to load driver. {e}")
|
||||||
Reference in New Issue
Block a user