Spaces:
Sleeping
Sleeping
File size: 12,534 Bytes
08c80af 631c799 08c80af 12e2c64 08c80af e506679 08c80af a6058bc 08c80af f2ec7d4 08c80af 12e2c64 08c80af 12e2c64 08c80af 12e2c64 08c80af 12e2c64 08c80af c17cf76 08c80af c17cf76 08c80af f2ec7d4 08c80af 12e2c64 08c80af 7376a17 08c80af 12e2c64 08c80af |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
from io import BytesIO
from bs4 import BeautifulSoup
from collections import namedtuple
import requests
import re
import pandas as pd
import numpy as np
import time
import streamlit as st
prezzo_al_mq = 0
class Immobiliare:
def __init__(self, url, *,
verbose=True,
min_house_cost=10_000,
browse_all_pages=True,
area_not_found=0,
price_not_found=np.nan,
floor_not_found=0,
car_not_found=0,
energy_not_found="n/a",
invalid_price_per_area=0,
wait=60):
self.url = url
self.verbose = verbose
self.min_house_cost = min_house_cost
self.browse_all_pages = browse_all_pages
self.wait = wait / 1000
self.area_not_found = area_not_found
self.price_not_found = price_not_found
self.floor_not_found = floor_not_found
self.car_not_found = car_not_found
self.energy_not_found = energy_not_found
self.invalid_price_per_area = invalid_price_per_area
def _say(self, *args, **kwargs):
if self.verbose:
print(*args, **kwargs)
def get_all_urls(self):
pattern = re.compile(r"\d+\/$")
urls_ = []
# first page
self._say("Processing page 1")
page = self._get_page(self.url)
page.seek(0)
soup = BeautifulSoup(page, "html.parser")
for link in soup.find_all("a"):
time.sleep(self.wait)
l = link.get("href")
if l is None:
continue
if "https" in l and "annunci" in l:
if pattern.search(l):
urls_.append(l)
if self.browse_all_pages:
for i in range(2, 10_000):
self._say(f"Processing page {i}")
curr_url = self.url + f"&pag={i}"
t = self._get_text(curr_url).lower()
if "404 not found" in t or "non è presente" in t:
self.urls_ = urls_
break
else:
page = self._get_page(curr_url)
page.seek(0)
soup = BeautifulSoup(page, "html.parser")
for link in soup.find_all("a"):
l = link.get("href")
if l is None:
continue
if "https" in l and "annunci" in l:
if pattern.search(l):
urls_.append(l)
self.urls_ = urls_
self._say("All retrieved urls in attribute 'urls_'")
self._say(f"Found {len(urls_)} houses matching criteria.")
@staticmethod
def _get_page(url):
req = requests.get(url, allow_redirects=False)
page = BytesIO()
page.write(req.content)
return page
@staticmethod
def _get_text(sub_url):
req = requests.get(sub_url, allow_redirects=False)
page = BytesIO()
page.write(req.content)
page.seek(0)
soup = BeautifulSoup(page, "html.parser")
text = soup.get_text()
t = text.replace("\n", "")
for _ in range(50):
t = t.replace(" ", " ")
return t
def _get_data(self, sub_url):
t = self._get_text(sub_url).lower()
# costo appartamento
cost_patterns = (
r"€ (\d+\.\d+\.\d+)", #if that's more than 1M €
r"€ (\d+\.\d+)",
)
cost = None
locali = None
for pattern in cost_patterns:
cost_pattern = re.compile(pattern)
try:
cost = cost_pattern.search(t)
locali = str(cost.group(1).replace(".", ""))[-1]
cost = str(cost.group(1).replace(".", ""))[:-1]
#cost = cost.group(1).replace(".", "")
break
except AttributeError:
continue
if cost is None:
if "prezzo su richiesta" in t:
self._say(f"Price available upon request for {sub_url}")
cost = self.price_not_found
else:
self._say(f"Can't get price for {sub_url}")
cost = self.price_not_found
if cost is not None and cost is not self.price_not_found:
if int(cost) < self.min_house_cost:
if "prezzo su richiesta" in t:
self._say(f"Price available upon request for {sub_url}")
cost = self.price_not_found
else:
self._say(f"Too low house price: {int(cost)}? for {sub_url}")
cost = self.price_not_found
# piano
floor_patterns = (
r"piano (\d{1,2})",
r"(\d{1,2}) piano",
r"(\d{1,2}) piani",
)
floor = None
for pattern in floor_patterns:
floor_pattern = re.compile(pattern)
floor = floor_pattern.search(t)
if floor is not None:
floor = floor.group(1)
break
if "piano terra" in t:
floor = 1
ultimo = "ultimo" in t
# metri quadri
area_pattern = re.compile(r"(\d+) m²")
try:
area = area_pattern.search(t)
area = area.group(1)
except AttributeError:
area = self.area_not_found
if "asta" in t:
self._say(f"Auction house: no area info {sub_url}")
else:
self._say(f"Can't get area info from url {sub_url}")
# classe energetica
energy_patterns = (
r"energetica (\D{1,2}) ",
r"energetica(\S{1,2})",
)
def energy_acceptable(stringlike):
if not stringlike.startswith(("A", "B", "C", "D", "E", "F", "G")):
return False
else:
if len(stringlike) == 1:
return True
else:
if not stringlike.endswith(
("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+")
):
return False
else:
return True
energy = None
for i, pattern in enumerate(energy_patterns):
energy_pattern = re.compile(pattern)
energy = energy_pattern.search(t)
if energy is not None:
energy = energy.group(1).upper()
if energy_acceptable(energy):
break
if energy is None or not energy_acceptable(energy):
if "in attesa di certificazione" in t:
self._say(f"Energy efficiency still pending for {sub_url} ")
energy = self.energy_not_found
else:
self._say(f"Can't get energy efficiency from {sub_url}")
energy = self.energy_not_found
# posto auto
car_patterns = (
r"post\S auto (\d{1,2})",
)
car = None
for pattern in car_patterns:
car_pattern = re.compile(pattern)
car = car_pattern.search(t)
if car is not None:
car = car.group(1)
break
if car is None:
available_upon_request = re.compile(r"possibilit\S.{0,10}auto")
if available_upon_request.search(t) is not None:
self._say(f"Car spot/box available upon request for {sub_url}")
car = 0
else:
car = self.car_not_found
# €/m²
try:
price_per_area = round(int(cost) / int(area), 1)
differenza = prezzo_al_mq - price_per_area
vantaggio = (differenza / prezzo_al_mq) * 120
vantaggio = max(0, vantaggio)
vantaggio = int(vantaggio)
except:
price_per_area = self.energy_not_found
vantaggio = 0
# packing the results
House = namedtuple(
"House", [
"Vantaggio",
"Prezzo_Mq",
"Prezzo",
"Superficie",
"Locali",
"Piano",
#"ultimo",
"Url"
#"energy",
#"posto_auto"
]
)
res = House(
vantaggio,
price_per_area,
cost,
area,
#ultimo,
locali,
floor,
sub_url
#energy,
#car
)
return res
def find_all_houses(self):
if not hasattr(self, "urls_"):
self.get_all_urls()
all_results = []
for url in self.urls_:
try:
all_results.append(self._get_data(url))
except:
print(f"offending_url='{url}'")
raise
self.df_ = pd.DataFrame(all_results)
self._say("Results stored in attribute 'df_'")
# Funzione di styling per evidenziare in rosso i valori inferiori alla variabile
def evidenzia_in_rosso(valore, soglia):
if valore < soglia:
return 'background-color: red; color: white'
return ''
st.set_page_config(layout="wide")
# Streamlit interface
st.title('🏠 Immobiliare A.I. ')
st.write("##### Il tuo assistente di intelligenza artificiale per la ricerca di occasioni immobiliari")
with st.expander("Informazioni"):
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!")
cerca_premuto = False
# Input field for 'comune'
with st.sidebar:
st.title("Filtri")
comune_input = st.text_input("Comune", 'lonato del garda')
prezzo_al_mq = st.number_input("Prezzo Medio al Mq", 2500)
prezzo_minimo = st.sidebar.slider("Prezzo Minimo", min_value=0, max_value=1000, value=200)
prezzo_massimo = st.sidebar.slider("Prezzo Massimo", min_value=0, max_value=1000, value=230)
locali = list(range(1, 21)) # Intervallo da 1 a 10
# Select slider unico per selezionare l'intervallo del numero di locali
locali_range = st.sidebar.select_slider(
"Locali",
options=locali,
value=(locali[2], locali[4]) # Valore iniziale, da 1 a 5 locali
)
# Dividi il range in minimo e massimo numero di locali
locali_minimo, locali_massimo = locali_range
prezzo_minimo = prezzo_minimo*1000
prezzo_massimo = prezzo_massimo*1000
cerca_premuto = st.button("Cerca", use_container_width=True, type='primary')
if cerca_premuto:
if comune_input:
comune = comune_input.replace(" ", "-")
url = f"https://www.immobiliare.it/vendita-case/{comune}/?prezzoMinimo={prezzo_minimo}&prezzoMassimo={prezzo_massimo}&localiMinimo={locali_minimo}&localiMassimo={locali_massimo}&random=123456"
#st.write(f"Seraching: {url}")
with st.spinner("Ricerca immobiliare in corso..."):
case = Immobiliare(url)
case.find_all_houses()
df = case.df_
df = df.sort_values(by="Prezzo_Mq", ascending=True)
st.dataframe(df, hide_index=True, use_container_width=True,
column_config ={
"Vantaggio": st.column_config.ProgressColumn(
"Vantaggio",
help="Vantaggio in %",
format='%f',
min_value=0,
max_value=100,
),
"Prezzo_Mq": " €/Mq",
"Prezzo": "Prezzo Totale",
"Superficie": "Superficie",
"Locali": "Locali",
"Piano": "Piano",
"Url": st.column_config.LinkColumn("App URL")
})
st.success("Elaborazione Completata")
else:
st.error("Per favore, inserisci il nome di un comune.") |