Spaces:
Runtime error
Runtime error
Update app.py
Browse files19 unmodified lines202122232425265 unmodified lines3233343536373839404142434445464748495051525354555637 unmodified lines94959697989910029 unmodified lines13013113213313413513670 unmodified lines20720820921021121221321421519 unmodified linessubprocess.run(["pip", "install", "fake-useragent"], check=True)from fake_useragent import UserAgent# --- 1. GENERADOR DE URLS (CORREGIDO MC Y FR) ---def construir_urls_final(zona, ciudad, tipo, hab, ban, park, antiguedad):mapa_ant = {"Menos de 1 año": "de-0-a-1-anos",5 unmodified linesslug_ant = mapa_ant.get(antiguedad, "de-1-a-8-anios")slug_park = f"{int(park)}-parqueadero" if int(park) == 1 else f"{int(park)}-parqueaderos"# Limpieza estricta de slugsz_slug = zona.lower().strip().replace(" ", "-")c_slug = ciudad.lower().strip().replace(" ", "-")# URL FINCA RAÍZurl_fr = f"https://www.fincaraiz.com.co/arriendo/{z_slug}/{c_slug}/{int(hab)}-o-mas-habitaciones/{int(ban)}-o-mas-banos/{slug_park}/{slug_ant}"# URL METROCUADRADO CORREGIDA (Se añade {z_slug})url_mc = f"https://www.metrocuadrado.com/{tipo.lower()}-casa-oficina/arriendo/{c_slug}/{z_slug}/{int(ban)}-banos-{int(hab)}-habitaciones/?search=form"return url_fr, url_mc# --- 2. EXTRACTOR DE PRECIO (REGEX) ---def extraer_precio_regex(texto):patron = r'\$\s?(\d{1,3}(?:[.,]\d{3})*)'coincidencias = re.findall(patron, texto)if coincidencias:precios = [int(p.replace('.', '').replace(',', '')) for p in coincidencias]return max(precios)return 0# --- 3. MOTOR DE EXTRACCIÓN ROBUSTO ---37 unmodified linesif "$" in txt:precio = extraer_precio_regex(txt)if precio > 600000:href = el.get_attribute("href")full_url = f"https://www.fincaraiz.com.co{href}" if href and href.startswith("/") else href29 unmodified linesif "$" in txt:precio = extraer_precio_regex(txt)if precio > 600000:enlace = card.query_selector("a")if enlace:href = enlace.get_attribute("href")70 unmodified lineswith gr.Row():with gr.Column(scale=1):c = gr.Textbox(label="Ciudad", value="Bogota")z = gr.Textbox(label="Zona (Ej: Usaquen)", value="Usaquen")a = gr.Number(label="Área M2", value=60)t = gr.Dropdown(["Apartamento", "Casa"], label="Tipo", value="Apartamento")with gr.Row():19 unmodified lines202122232425265 unmodified lines3233343536373839404142434445464748495051525354555657585937 unmodified lines97989910010110210329 unmodified lines13313413513613713813970 unmodified lines21021121221321421521621721819 unmodified linessubprocess.run(["pip", "install", "fake-useragent"], check=True)from fake_useragent import UserAgent# --- 1. GENERADOR DE URLS ---def construir_urls_final(zona, ciudad, tipo, hab, ban, park, antiguedad):mapa_ant = {"Menos de 1 año": "de-0-a-1-anos",5 unmodified linesslug_ant = mapa_ant.get(antiguedad, "de-1-a-8-anios")slug_park = f"{int(park)}-parqueadero" if int(park) == 1 else f"{int(park)}-parqueaderos"z_slug = zona.lower().strip().replace(" ", "-")c_slug = ciudad.lower().strip().replace(" ", "-")url_fr = f"https://www.fincaraiz.com.co/arriendo/{z_slug}/{c_slug}/{int(hab)}-o-mas-habitaciones/{int(ban)}-o-mas-banos/{slug_park}/{slug_ant}"url_mc = f"https://www.metrocuadrado.com/{tipo.lower()}-casa-oficina/arriendo/{c_slug}/{z_slug}/{int(ban)}-banos-{int(hab)}-habitaciones/?search=form"return url_fr, url_mc# --- 2. EXTRACTOR DE PRECIO INTELIGENTE (REGEX) ---def extraer_precio_regex(texto):patron = r'\$\s?(\d{1,3}(?:[.,]\d{3})*)'coincidencias = re.findall(patron, texto)if coincidencias:precios = [int(p.replace('.', '').replace(',', '')) for p in coincidencias]# FILTRO DE CORDURA: Ignorar precios de venta (> 40 millones)# y descartar valores muy bajos (como la administración sola)precios_validos = [p for p in precios if 600000 <= p <= 40000000]if precios_validos:# El primer precio válido que aparece en el texto es casi siempre el Canon principalreturn precios_validos[0]return 0# --- 3. MOTOR DE EXTRACCIÓN ROBUSTO ---37 unmodified linesif "$" in txt:precio = extraer_precio_regex(txt)if precio > 0: # Ya pasó por el filtro de cordurahref = el.get_attribute("href")full_url = f"https://www.fincaraiz.com.co{href}" if href and href.startswith("/") else href29 unmodified linesif "$" in txt:precio = extraer_precio_regex(txt)if precio > 0:enlace = card.query_selector("a")if enlace:href = enlace.get_attribute("href")70 unmodified lineswith gr.Row():with gr.Column(scale=1):c = gr.Textbox(label="Ciudad", value="Barranquilla")z = gr.Textbox(label="Zona (Ej: El Prado)", value="El Prado")a = gr.Number(label="Área M2", value=70)t = gr.Dropdown(["Apartamento", "Casa"], label="Tipo", value="Apartamento")with gr.Row():
app.py+15-12