├── .gitignore ├── README.md ├── selenium_scraping_example.py └── tiempo_hoy.csv /.gitignore: -------------------------------------------------------------------------------- 1 | /data/ 2 | data/ 3 | data 4 | /.idea/ 5 | .idea/ 6 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Selenium con Python 2 | 3 | _Tutorial básico de Selenium en Python. Como hacer Web Scraping con Selenium en Python_ 4 | 5 | 6 | ### Pre-requisitos 📋 7 | 8 | _Tener instalado en nestro equipo Python_ 9 | 10 | _Librerías de Python utilizadas:_ 11 | 12 | ``` 13 | - Selenium 14 | - Pandas 15 | ``` 16 | 17 | ### Instalación 🔧 18 | 19 | _Para instalar Python:_ 20 | 21 | 22 | ``` 23 | https://www.python.org/downloads/ 24 | ``` 25 | 26 | _Para instalar librerías:_ 27 | 28 | ``` 29 | https://youtu.be/HlmUaYKjjZc 30 | ``` 31 | 32 | ## Autores ✒️ 33 | 34 | _Autores del proyecto:_ 35 | 36 | * **Errodringer** - *Programación y edición* - [Errodringer](https://www.youtube.com/c/Errodringer?sub_confirmation=1) 37 | 38 | ## Licencia 📄 39 | 40 | Este proyecto es divulgativo. Solo para uso personal, aprendizaje y entretenimiento. 41 | 42 | ## Expresiones de Gratitud 🎁 43 | 44 | * Muchas gracias a todos! 45 | * Comenta a otros sobre este proyecto y difunde! 📢 46 | * 🤓 47 | -------------------------------------------------------------------------------- /selenium_scraping_example.py: -------------------------------------------------------------------------------- 1 | # Librerías 2 | from selenium import webdriver 3 | from selenium.webdriver.support.ui import WebDriverWait 4 | from selenium.webdriver.support import expected_conditions as EC 5 | from selenium.webdriver.common.by import By 6 | import time 7 | import pandas as pd 8 | 9 | # Opciones de navegación 10 | options = webdriver.ChromeOptions() 11 | options.add_argument('--start-maximized') 12 | options.add_argument('--disable-extensions') 13 | 14 | driver_path = 'C:\\Users\\rodgo\\Downloads\\chromedriver_win32\\chromedriver.exe' 15 | 16 | driver = webdriver.Chrome(driver_path, chrome_options=options) 17 | 18 | # Iniciarla en la pantalla 2 19 | driver.set_window_position(2000, 0) 20 | driver.maximize_window() 21 | time.sleep(1) 22 | 23 | # Inicializamos el navegador 24 | driver.get('https://eltiempo.es') 25 | 26 | WebDriverWait(driver, 5)\ 27 | .until(EC.element_to_be_clickable((By.CSS_SELECTOR, 28 | 'button.didomi-components-button didomi-button didomi-dismiss-button didomi-components-button--color didomi-button-highlight highlight-button'.replace(' ', '.'))))\ 29 | .click() 30 | 31 | WebDriverWait(driver, 5)\ 32 | .until(EC.element_to_be_clickable((By.CSS_SELECTOR, 33 | 'input#inputSearch')))\ 34 | .send_keys('Madrid') 35 | 36 | WebDriverWait(driver, 5)\ 37 | .until(EC.element_to_be_clickable((By.CSS_SELECTOR, 38 | 'i.icon.icon-search')))\ 39 | .click() 40 | 41 | WebDriverWait(driver, 5)\ 42 | .until(EC.element_to_be_clickable((By.CSS_SELECTOR, 43 | 'i.icon_weather_s.icon.icon-local')))\ 44 | .click() 45 | 46 | WebDriverWait(driver, 5)\ 47 | .until(EC.element_to_be_clickable((By.XPATH, 48 | '/html/body/div[7]/main/div[4]/div/section[4]/section/div/article/section/ul/li[2]/a')))\ 49 | .click() 50 | 51 | 52 | WebDriverWait(driver, 5)\ 53 | .until(EC.element_to_be_clickable((By.XPATH, 54 | '/html/body/div[7]/main/div[4]/div/section[4]/section/div[1]/ul'))) 55 | 56 | texto_columnas = driver.find_element_by_xpath('/html/body/div[7]/main/div[4]/div/section[4]/section/div[1]/ul') 57 | texto_columnas = texto_columnas.text 58 | 59 | tiempo_hoy = texto_columnas.split('Mañana')[0].split('\n')[1:-1] 60 | 61 | horas = list() 62 | temp = list() 63 | v_viento = list() 64 | 65 | for i in range(0, len(tiempo_hoy), 4): 66 | horas.append(tiempo_hoy[i]) 67 | temp.append(tiempo_hoy[i+1]) 68 | v_viento.append(tiempo_hoy[i+2]) 69 | 70 | df = pd.DataFrame({'Horas': horas, 'Temperatura': temp, 'V_viento(km_h)':v_viento}) 71 | print(df) 72 | df.to_csv('tiempo_hoy.csv', index=False) 73 | 74 | driver.quit() 75 | 76 | -------------------------------------------------------------------------------- /tiempo_hoy.csv: -------------------------------------------------------------------------------- 1 | Horas,Temperatura,V_viento(km_h) 2 | 14,14°,4 3 | 15,14°,3 4 | 16,14°,3 5 | 17,14°,3 6 | 18,13°,3 7 | 19,12°,4 8 | 20,11°,4 9 | 21,11°,4 10 | 22,10°,4 11 | 23,10°,4 12 | --------------------------------------------------------------------------------