-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
63 lines (40 loc) · 2.06 KB
/
scraper.py
File metadata and controls
63 lines (40 loc) · 2.06 KB
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
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
def scrape_maps(search_query, total_results=20):
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
try:
print(f"🚀 Iniciando búsqueda: {search_query}...")
driver.get(f"https://www.google.com/maps/search/{search_query}")
time.sleep(5)
results = []
listings = driver.find_elements(By.CLASS_NAME, "hfpxzc")
print(f"📋 Encontrados {len(listings)} resultados preliminares. Extrayendo datos...")
for i, list_item in enumerate(listings[:total_results]):
try:
name = list_item.get_attribute("aria-label")
driver.execute_script("arguments[0].scrollIntoView();", list_item)
print(f"✅ [{i+1}] Extraído: {name}")
results.append({"Nombre": name, "Estado": "Contacto Pendiente"})
time.sleep(1)
except Exception as e:
print(f"❌ Error en el item {i}: {e}")
df = pd.DataFrame(results)
df.to_csv("clientes_potenciales.csv", index=False, encoding='utf-8-sig')
print("\n" + "="*40)
print("🔥 PROCESO FINALIZADO CON ÉXITO")
print("📁 Archivo generado: clientes_potenciales.csv")
print("⚠️ La ventana de Chrome quedará abierta para tu grabación.")
print("="*40)
except Exception as e:
print(f"🔴 Error general: {e}")
if __name__ == "__main__":
query = "Restaurantes en Madrid"
scrape_maps(query)