Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,326 +1,146 @@
|
|
| 1 |
-
from io import BytesIO
|
| 2 |
-
from bs4 import BeautifulSoup
|
| 3 |
-
from collections import namedtuple
|
| 4 |
import requests
|
|
|
|
|
|
|
| 5 |
import re
|
| 6 |
import pandas as pd
|
|
|
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
-
import time
|
| 9 |
import streamlit as st
|
| 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 |
-
urls_ = []
|
| 47 |
-
|
| 48 |
-
# first page
|
| 49 |
-
self._say("Processing page 1")
|
| 50 |
-
page = self._get_page(self.url)
|
| 51 |
-
|
| 52 |
-
page.seek(0)
|
| 53 |
-
soup = BeautifulSoup(page, "html.parser")
|
| 54 |
-
|
| 55 |
-
for link in soup.find_all("a"):
|
| 56 |
-
time.sleep(self.wait)
|
| 57 |
-
l = link.get("href")
|
| 58 |
-
|
| 59 |
-
if l is None:
|
| 60 |
-
continue
|
| 61 |
-
|
| 62 |
-
if "https" in l and "annunci" in l:
|
| 63 |
-
if pattern.search(l):
|
| 64 |
-
urls_.append(l)
|
| 65 |
-
|
| 66 |
-
if self.browse_all_pages:
|
| 67 |
-
for i in range(2, 10_000):
|
| 68 |
-
self._say(f"Processing page {i}")
|
| 69 |
-
curr_url = self.url + f"&pag={i}"
|
| 70 |
-
|
| 71 |
-
t = self._get_text(curr_url).lower()
|
| 72 |
-
|
| 73 |
-
if "404 not found" in t or "non è presente" in t:
|
| 74 |
-
self.urls_ = urls_
|
| 75 |
-
break
|
| 76 |
-
|
| 77 |
-
else:
|
| 78 |
-
page = self._get_page(curr_url)
|
| 79 |
-
page.seek(0)
|
| 80 |
-
soup = BeautifulSoup(page, "html.parser")
|
| 81 |
-
|
| 82 |
-
for link in soup.find_all("a"):
|
| 83 |
-
l = link.get("href")
|
| 84 |
-
|
| 85 |
-
if l is None:
|
| 86 |
-
continue
|
| 87 |
-
|
| 88 |
-
if "https" in l and "annunci" in l:
|
| 89 |
-
if pattern.search(l):
|
| 90 |
-
urls_.append(l)
|
| 91 |
-
|
| 92 |
-
self.urls_ = urls_
|
| 93 |
-
self._say("All retrieved urls in attribute 'urls_'")
|
| 94 |
-
self._say(f"Found {len(urls_)} houses matching criteria.")
|
| 95 |
-
|
| 96 |
-
@staticmethod
|
| 97 |
-
def _get_page(url):
|
| 98 |
-
req = requests.get(url, allow_redirects=False)
|
| 99 |
-
page = BytesIO()
|
| 100 |
-
page.write(req.content)
|
| 101 |
-
return page
|
| 102 |
-
|
| 103 |
-
@staticmethod
|
| 104 |
-
def _get_text(sub_url):
|
| 105 |
-
req = requests.get(sub_url, allow_redirects=False)
|
| 106 |
-
page = BytesIO()
|
| 107 |
-
page.write(req.content)
|
| 108 |
-
page.seek(0)
|
| 109 |
-
soup = BeautifulSoup(page, "html.parser")
|
| 110 |
-
text = soup.get_text()
|
| 111 |
-
t = text.replace("\n", "")
|
| 112 |
-
for _ in range(50):
|
| 113 |
-
t = t.replace(" ", " ")
|
| 114 |
-
return t
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
if
|
| 167 |
-
floor = floor.group(1)
|
| 168 |
-
break
|
| 169 |
-
|
| 170 |
-
if "piano terra" in t:
|
| 171 |
-
floor = 1
|
| 172 |
-
|
| 173 |
-
ultimo = "ultimo" in t
|
| 174 |
-
|
| 175 |
-
# metri quadri
|
| 176 |
-
|
| 177 |
-
area_pattern = re.compile(r"(\d+) m²")
|
| 178 |
-
try:
|
| 179 |
-
area = area_pattern.search(t)
|
| 180 |
-
area = area.group(1)
|
| 181 |
-
except AttributeError:
|
| 182 |
-
area = self.area_not_found
|
| 183 |
-
if "asta" in t:
|
| 184 |
-
self._say(f"Auction house: no area info {sub_url}")
|
| 185 |
-
else:
|
| 186 |
-
self._say(f"Can't get area info from url {sub_url}")
|
| 187 |
-
|
| 188 |
-
# classe energetica
|
| 189 |
-
energy_patterns = (
|
| 190 |
-
r"energetica (\D{1,2}) ",
|
| 191 |
-
r"energetica(\S{1,2})",
|
| 192 |
-
)
|
| 193 |
-
|
| 194 |
-
def energy_acceptable(stringlike):
|
| 195 |
-
if not stringlike.startswith(("A", "B", "C", "D", "E", "F", "G")):
|
| 196 |
-
return False
|
| 197 |
-
else:
|
| 198 |
-
if len(stringlike) == 1:
|
| 199 |
-
return True
|
| 200 |
-
else:
|
| 201 |
-
if not stringlike.endswith(
|
| 202 |
-
("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+")
|
| 203 |
-
):
|
| 204 |
-
return False
|
| 205 |
-
else:
|
| 206 |
-
return True
|
| 207 |
-
|
| 208 |
-
energy = None
|
| 209 |
-
for i, pattern in enumerate(energy_patterns):
|
| 210 |
-
energy_pattern = re.compile(pattern)
|
| 211 |
-
energy = energy_pattern.search(t)
|
| 212 |
-
if energy is not None:
|
| 213 |
-
energy = energy.group(1).upper()
|
| 214 |
-
if energy_acceptable(energy):
|
| 215 |
-
break
|
| 216 |
-
|
| 217 |
-
if energy is None or not energy_acceptable(energy):
|
| 218 |
-
if "in attesa di certificazione" in t:
|
| 219 |
-
self._say(f"Energy efficiency still pending for {sub_url} ")
|
| 220 |
-
energy = self.energy_not_found
|
| 221 |
-
else:
|
| 222 |
-
self._say(f"Can't get energy efficiency from {sub_url}")
|
| 223 |
-
energy = self.energy_not_found
|
| 224 |
-
|
| 225 |
-
# posto auto
|
| 226 |
-
car_patterns = (
|
| 227 |
-
r"post\S auto (\d{1,2})",
|
| 228 |
-
)
|
| 229 |
-
|
| 230 |
-
car = None
|
| 231 |
-
for pattern in car_patterns:
|
| 232 |
-
car_pattern = re.compile(pattern)
|
| 233 |
-
car = car_pattern.search(t)
|
| 234 |
-
if car is not None:
|
| 235 |
-
car = car.group(1)
|
| 236 |
break
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
"Prezzo",
|
| 264 |
-
"Superficie",
|
| 265 |
-
"Locali",
|
| 266 |
-
"Piano",
|
| 267 |
-
#"ultimo",
|
| 268 |
-
"Url"
|
| 269 |
-
#"energy",
|
| 270 |
-
#"posto_auto"
|
| 271 |
-
]
|
| 272 |
-
)
|
| 273 |
-
|
| 274 |
-
res = House(
|
| 275 |
-
vantaggio,
|
| 276 |
-
price_per_area,
|
| 277 |
-
cost,
|
| 278 |
-
area,
|
| 279 |
-
#ultimo,
|
| 280 |
-
locali,
|
| 281 |
-
floor,
|
| 282 |
-
sub_url
|
| 283 |
-
#energy,
|
| 284 |
-
#car
|
| 285 |
-
)
|
| 286 |
-
|
| 287 |
-
return res
|
| 288 |
-
|
| 289 |
-
def find_all_houses(self):
|
| 290 |
-
if not hasattr(self, "urls_"):
|
| 291 |
-
self.get_all_urls()
|
| 292 |
-
|
| 293 |
-
all_results = []
|
| 294 |
-
for url in self.urls_:
|
| 295 |
-
try:
|
| 296 |
-
all_results.append(self._get_data(url))
|
| 297 |
-
except:
|
| 298 |
-
print(f"offending_url='{url}'")
|
| 299 |
-
raise
|
| 300 |
-
|
| 301 |
-
self.df_ = pd.DataFrame(all_results)
|
| 302 |
-
self._say("Results stored in attribute 'df_'")
|
| 303 |
-
|
| 304 |
-
# Funzione di styling per evidenziare in rosso i valori inferiori alla variabile
|
| 305 |
-
def evidenzia_in_rosso(valore, soglia):
|
| 306 |
-
if valore < soglia:
|
| 307 |
-
return 'background-color: red; color: white'
|
| 308 |
-
return ''
|
| 309 |
|
| 310 |
st.set_page_config(layout="wide")
|
| 311 |
-
# Streamlit interface
|
| 312 |
|
| 313 |
st.title('🏠 Immobiliare A.I. ')
|
| 314 |
st.write("##### Il tuo assistente di intelligenza artificiale per la ricerca di occasioni immobiliari")
|
| 315 |
with st.expander("Informazioni"):
|
| 316 |
st.write("Immobiliare A.I. è la webapp che semplifica la ricerca di immobili, grazie a algoritmi avanzati che calcolano il vantaggio di ogni offerta. Trova le migliori occasioni sul mercato con analisi precise e personalizzate. Scopri l’immobile giusto per te con facilità e sicurezza!")
|
| 317 |
-
|
| 318 |
cerca_premuto = False
|
| 319 |
-
|
|
|
|
| 320 |
with st.sidebar:
|
|
|
|
| 321 |
st.title("Filtri")
|
| 322 |
-
|
| 323 |
-
|
|
|
|
|
|
|
|
|
|
| 324 |
prezzo_minimo = st.sidebar.slider("Prezzo Minimo", min_value=0, max_value=1000, value=200)
|
| 325 |
prezzo_massimo = st.sidebar.slider("Prezzo Massimo", min_value=0, max_value=1000, value=230)
|
| 326 |
|
|
@@ -339,35 +159,65 @@ with st.sidebar:
|
|
| 339 |
prezzo_massimo = prezzo_massimo*1000
|
| 340 |
cerca_premuto = st.button("Cerca", use_container_width=True, type='primary')
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
df =
|
| 353 |
-
df = df.sort_values(by="Prezzo_Mq", ascending=True)
|
| 354 |
-
|
| 355 |
st.dataframe(df, hide_index=True, use_container_width=True,
|
| 356 |
column_config ={
|
|
|
|
| 357 |
"Vantaggio": st.column_config.ProgressColumn(
|
| 358 |
-
"
|
| 359 |
help="Vantaggio in %",
|
| 360 |
format='%f',
|
| 361 |
min_value=0,
|
| 362 |
max_value=100,
|
| 363 |
),
|
| 364 |
-
"
|
| 365 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
"Superficie": "Superficie",
|
| 367 |
"Locali": "Locali",
|
| 368 |
-
"
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import json
|
| 4 |
import re
|
| 5 |
import pandas as pd
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from collections import namedtuple
|
| 8 |
import numpy as np
|
|
|
|
| 9 |
import streamlit as st
|
| 10 |
|
| 11 |
+
def clean_text(text):
|
| 12 |
+
return re.sub(r'\s+', ' ', text).strip()
|
| 13 |
+
|
| 14 |
+
def formatta_numero(stringa):
|
| 15 |
+
stringa = stringa.split(',')[0]
|
| 16 |
+
stringa = stringa.replace(".", "").replace("da", "").replace("€", "").replace("%","").replace("m²", "").replace("locali", "").strip()
|
| 17 |
+
stringa = stringa.split(' ')[0]
|
| 18 |
+
return stringa
|
| 19 |
+
|
| 20 |
+
# Estrae le informazioni dagli ANNUNCI
|
| 21 |
+
def extract_info(provincia, comune, prezzo_medio_mq, listing):
|
| 22 |
+
info = {}
|
| 23 |
+
superficie = ""
|
| 24 |
+
locali = ""
|
| 25 |
+
info['Provincia'] = provincia
|
| 26 |
+
info['Comune'] = comune
|
| 27 |
+
price_elem = listing.find('div', class_='in-listingCardPrice')
|
| 28 |
+
prezzo = clean_text(price_elem.text) if price_elem else ""
|
| 29 |
+
link_elem = listing.find('a', class_='in-listingCardTitle')
|
| 30 |
+
if link_elem:
|
| 31 |
+
link = link_elem['href']
|
| 32 |
+
titolo = link_elem.text.strip()
|
| 33 |
+
feature_list = listing.find('div', class_='in-listingCardFeatureList')
|
| 34 |
+
if feature_list:
|
| 35 |
+
for item in feature_list.find_all('div', class_='in-listingCardFeatureList__item'):
|
| 36 |
+
use_elem = item.find('use', class_='nd-icon__use')
|
| 37 |
+
if use_elem:
|
| 38 |
+
if use_elem.get('xlink:href') == '#planimetry':
|
| 39 |
+
locali = item.find('span').text.strip()
|
| 40 |
+
elif use_elem.get('xlink:href') == '#size':
|
| 41 |
+
superficie = item.find('span').text.strip()
|
| 42 |
+
image_url = ""
|
| 43 |
+
img = listing.find('figure', class_='nd-figure nd-ratio in-photo')
|
| 44 |
+
if img:
|
| 45 |
+
image_url = img.find('img')['src']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
superficie = formatta_numero(superficie)
|
| 48 |
+
locali = formatta_numero(locali)
|
| 49 |
+
prezzo = formatta_numero(prezzo)
|
| 50 |
+
info['Immagine'] = image_url
|
| 51 |
+
info['Titolo'] = titolo
|
| 52 |
+
info['Prezzo'] = int(prezzo)
|
| 53 |
+
info['Superficie'] = int(superficie)
|
| 54 |
+
try:
|
| 55 |
+
prezzo_numerico = int(prezzo)
|
| 56 |
+
superficie_numerica = int(superficie)
|
| 57 |
+
info['PrezzoMq'] = prezzo_numerico // superficie_numerica
|
| 58 |
+
prezzo_medio_mq = formatta_numero(prezzo_medio_mq)
|
| 59 |
+
prezzo_medio_mq_numerico = int(prezzo_medio_mq)
|
| 60 |
+
differenza = prezzo_medio_mq_numerico - info['PrezzoMq']
|
| 61 |
+
vantaggio = (differenza / prezzo_medio_mq_numerico) * 100
|
| 62 |
+
vantaggio = max(0, vantaggio)
|
| 63 |
+
vantaggio = int(vantaggio)
|
| 64 |
+
except (ValueError, ZeroDivisionError):
|
| 65 |
+
info['PrezzoMq'] = 0
|
| 66 |
+
vantaggio = 0
|
| 67 |
+
info['Locali'] = int(locali)
|
| 68 |
+
info['Link'] = link
|
| 69 |
+
info['PrezzoMedioMq'] = int(prezzo_medio_mq)
|
| 70 |
+
info['Vantaggio'] = vantaggio
|
| 71 |
+
if info['PrezzoMq']< int(prezzo_medio_mq) and info['PrezzoMq']>0:
|
| 72 |
+
info['Vantaggioso'] = True
|
| 73 |
+
else:
|
| 74 |
+
info['Vantaggioso'] = False
|
| 75 |
+
return info
|
| 76 |
+
|
| 77 |
+
# Legge gli ANNUNCI (pagina x pagina) sulla base del COMUNE di appartenenza
|
| 78 |
+
def scrape_immobiliare(provincia, comune, prezzo_medio_mq, prezzo_minimo, prezzo_massimo, locali_minimo, locali_massimo):
|
| 79 |
+
print(provincia + " " + comune + " " + prezzo_medio_mq)
|
| 80 |
+
comune_url = comune.replace(" ", "-")
|
| 81 |
+
base_url = f"https://www.immobiliare.it/vendita-case/{comune_url}/?prezzoMinimo={prezzo_minimo}&prezzoMassimo={prezzo_massimo}&localiMinimo={locali_minimo}&localiMassimo={locali_massimo}&random=123456"
|
| 82 |
+
results = []
|
| 83 |
+
page = 1
|
| 84 |
+
url = base_url
|
| 85 |
+
while True:
|
| 86 |
+
print(f'Elaborazione pagina {page}')
|
| 87 |
+
response = requests.get(url)
|
| 88 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 89 |
+
listings = soup.find_all('li', class_='nd-list__item in-searchLayoutListItem')
|
| 90 |
+
if not listings:
|
| 91 |
+
break
|
| 92 |
+
for listing in listings:
|
| 93 |
+
results.append(extract_info(provincia, comune, prezzo_medio_mq, listing))
|
| 94 |
+
pagination = soup.find('div', class_='in-pagination__list')
|
| 95 |
+
if pagination:
|
| 96 |
+
next_page = pagination.find('a', class_='in-pagination__item', string=lambda text: text and text.strip().isdigit())
|
| 97 |
+
if not next_page:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
break
|
| 99 |
+
page += 1
|
| 100 |
+
url = base_url + '&pag=' + str(page)
|
| 101 |
+
|
| 102 |
+
return json.dumps(results, ensure_ascii=False, indent=2)
|
| 103 |
+
|
| 104 |
+
# Restituisce l'elenco dei COMUNI di una Provincia e il PREZZO MEDIO
|
| 105 |
+
def get_elenco_comuni(provincia):
|
| 106 |
+
base_url = f"https://www.immobiliare.it/mercato-immobiliare/lombardia/{provincia}-provincia/"
|
| 107 |
+
results = []
|
| 108 |
+
print(f'Lettura Comuni e Prezzo Medio al Mq')
|
| 109 |
+
response = requests.get(base_url)
|
| 110 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 111 |
+
rows = soup.find_all('tr', class_='nd-table__row')
|
| 112 |
+
results = []
|
| 113 |
+
for row in rows:
|
| 114 |
+
cells = row.find_all('td', class_='nd-table__cell')
|
| 115 |
+
if len(cells) >= 2:
|
| 116 |
+
comune = cells[0].get_text(strip=True)
|
| 117 |
+
prezzo_vendita = cells[1].get_text(strip=True)
|
| 118 |
+
results.append({
|
| 119 |
+
'provincia': provincia,
|
| 120 |
+
'comune': comune,
|
| 121 |
+
'prezzo': prezzo_vendita
|
| 122 |
+
})
|
| 123 |
+
|
| 124 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
st.set_page_config(layout="wide")
|
|
|
|
| 127 |
|
| 128 |
st.title('🏠 Immobiliare A.I. ')
|
| 129 |
st.write("##### Il tuo assistente di intelligenza artificiale per la ricerca di occasioni immobiliari")
|
| 130 |
with st.expander("Informazioni"):
|
| 131 |
st.write("Immobiliare A.I. è la webapp che semplifica la ricerca di immobili, grazie a algoritmi avanzati che calcolano il vantaggio di ogni offerta. Trova le migliori occasioni sul mercato con analisi precise e personalizzate. Scopri l’immobile giusto per te con facilità e sicurezza!")
|
| 132 |
+
|
| 133 |
cerca_premuto = False
|
| 134 |
+
comuni_provincia = {}
|
| 135 |
+
|
| 136 |
with st.sidebar:
|
| 137 |
+
comuni_provincia = get_elenco_comuni('Brescia')
|
| 138 |
st.title("Filtri")
|
| 139 |
+
elenco = [d['comune'] for d in comuni_provincia]
|
| 140 |
+
comune_input = st.multiselect(
|
| 141 |
+
"Comuni",
|
| 142 |
+
elenco
|
| 143 |
+
)
|
| 144 |
prezzo_minimo = st.sidebar.slider("Prezzo Minimo", min_value=0, max_value=1000, value=200)
|
| 145 |
prezzo_massimo = st.sidebar.slider("Prezzo Massimo", min_value=0, max_value=1000, value=230)
|
| 146 |
|
|
|
|
| 159 |
prezzo_massimo = prezzo_massimo*1000
|
| 160 |
cerca_premuto = st.button("Cerca", use_container_width=True, type='primary')
|
| 161 |
|
| 162 |
+
#
|
| 163 |
+
#if __name__ == "__main__":
|
| 164 |
+
# print(df)
|
| 165 |
+
|
| 166 |
+
def scrivi_dataframe(output):
|
| 167 |
+
if len(output) > 0:
|
| 168 |
+
df = pd.DataFrame(output)
|
| 169 |
+
df = df.sort_values(by=["Vantaggio", "PrezzoMq"], ascending=[False, True])
|
| 170 |
+
columns_to_display = ["Vantaggioso", "Vantaggio", "Immagine", "Comune", "Titolo", "PrezzoMq", "Prezzo", "Superficie", "Locali", "PrezzoMedioMq", "Link"]
|
| 171 |
+
df = df[columns_to_display]
|
| 172 |
+
df = df.style.format(thousands='.')
|
|
|
|
|
|
|
| 173 |
st.dataframe(df, hide_index=True, use_container_width=True,
|
| 174 |
column_config ={
|
| 175 |
+
"Vantaggioso": st.column_config.CheckboxColumn("Vantaggioso"),
|
| 176 |
"Vantaggio": st.column_config.ProgressColumn(
|
| 177 |
+
"Punteggio",
|
| 178 |
help="Vantaggio in %",
|
| 179 |
format='%f',
|
| 180 |
min_value=0,
|
| 181 |
max_value=100,
|
| 182 |
),
|
| 183 |
+
"Immagine": st.column_config.ImageColumn("Anteprima", help="Anteprima", width="small"),
|
| 184 |
+
"PrezzoMq": "€/Mq",
|
| 185 |
+
"PrezzoMedioMq": "Media €/Mq",
|
| 186 |
+
"Prezzo": st.column_config.NumberColumn(
|
| 187 |
+
"Prezzo Totale",
|
| 188 |
+
help="Il prezzo totale dell'immobile in EURO",
|
| 189 |
+
step=1,
|
| 190 |
+
format="%d €",
|
| 191 |
+
),
|
| 192 |
"Superficie": "Superficie",
|
| 193 |
"Locali": "Locali",
|
| 194 |
+
"Link": st.column_config.LinkColumn("App URL")
|
| 195 |
+
})
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
if cerca_premuto:
|
| 200 |
+
comuni_selezionati = comune_input
|
| 201 |
+
comuni_selezionati = [comune.upper() for comune in comuni_selezionati]
|
| 202 |
+
output = []
|
| 203 |
+
output_singolo = []
|
| 204 |
+
for comune_provincia in comuni_provincia:
|
| 205 |
+
if comune_provincia['comune'].upper() in comuni_selezionati:
|
| 206 |
+
with st.spinner(f"Ricerca Immobili Comune: {comune_provincia['comune']}"):
|
| 207 |
+
output_singolo = json.loads(scrape_immobiliare(comune_provincia['provincia'],
|
| 208 |
+
comune_provincia['comune'],
|
| 209 |
+
comune_provincia['prezzo'],
|
| 210 |
+
prezzo_minimo,
|
| 211 |
+
prezzo_massimo,
|
| 212 |
+
locali_minimo,
|
| 213 |
+
locali_massimo))
|
| 214 |
+
st.write(f"### {comune_provincia['comune']}")
|
| 215 |
+
scrivi_dataframe(output_singolo)
|
| 216 |
+
st.divider()
|
| 217 |
+
output += output_singolo
|
| 218 |
+
if len(comuni_selezionati)>1:
|
| 219 |
+
st.write(f"### Comuni Selezionati")
|
| 220 |
+
scrivi_dataframe(output)
|
| 221 |
+
st.success("Elaborazione Completata")
|
| 222 |
+
else:
|
| 223 |
+
st.error("Per favore, inserisci il nome di un comune.")
|