-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (57 loc) · 3.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import csv
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://ecolana.com.mx/mapa")
nombre = []
residuos = []
direccion = []
telefono = []
horario = []
mapsUrl = []
try:
elements = WebDriverWait(browser, 10).until(
EC.presence_of_all_elements_located((By.XPATH, '//*[@id="map"]/div/div/div/div/div[2]/div[2]/div/div[3]/div/div/div/div/img')))
# elements = browser.find_elements(By.XPATH, '//*[@id="map"]/div/div/div/div/div[2]/div[2]/div/div[3]/div/div/div/div')
for element in elements:
# Ejecuta un click por javascript
browser.execute_script("arguments[0].click();", element)
# Esperar por puntos en el mapa
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[3]/div/div/div[2]/h2')))
nombre.append(browser.find_element(By.XPATH, '/html/body/div[3]/div/div/div[2]/h2').text)
residuos.append(browser.find_element(By.XPATH, '/html/body/div[3]/div/div/div[3]/div/div/div[1]/div[1]/p').text)
direccion.append(browser.find_element(By.XPATH, '/html/body/div[3]/div/div/div[3]/div/div/div[1]/div[2]/div[2]/p').text)
checkHor = browser.find_elements(By.XPATH, '/html/body/div[3]/div/div/div[3]/div/div/div[2]/div/div/div[1]/div/h4')
if(len(checkHor) != 0):
horario.append(browser.find_element(By.XPATH, '/html/body/div[3]/div/div/div[3]/div/div/div[2]/div/div/div[1]/div/h4').text)
else:
horario.append("")
checkTel = browser.find_elements(By.XPATH, '/html/body/div[3]/div/div/div[3]/div/div/div[1]/div[2]/div[4]/p')
if(len(checkTel) != 0):
telefono.append(browser.find_element(By.XPATH, '/html/body/div[3]/div/div/div[3]/div/div/div[1]/div[2]/div[4]/p').text)
else:
telefono.append("")
browser.find_element(By.XPATH, '/html/body/div[3]/div/div/div[3]/div/div/div[2]/div/div/div[2]/div[2]/img').click()
browser.switch_to.window(browser.window_handles[1])
try:
mapsUrl.append(browser.current_url)
except Exception as e:
print("fallo.")
if(len(nombre) != len(mapsUrl)):
mapsUrl.append(browser.current_url)
browser.close()
browser.switch_to.window(browser.window_handles[0])
browser.find_element(By.XPATH, '/html/body/div[3]/div/div/div[1]/button').click()
finally:
browser.quit()
archivo_csv = 'C:/Users/Fabia/OneDrive/Desktop/ecolanaData.csv'
with open(archivo_csv, mode='w', encoding='utf-8', newline='') as file:
writer = csv.writer(file)
# Titulos en csv
writer.writerow(['Nombre', 'Residuos', 'Dirección', 'Teléfono', 'Horario de servicio', 'URL'])
# Datos
for i in range(len(nombre)):
writer.writerow([nombre[i], residuos[i], direccion[i], telefono[i], horario[i], mapsUrl[i]])