-
Notifications
You must be signed in to change notification settings - Fork 2
/
Scrape_immobiliare.py
439 lines (333 loc) · 11.4 KB
/
Scrape_immobiliare.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import requests
from bs4 import BeautifulSoup
import pandas as pd
#from tqdm import tqdm_notebook as tqdm
from tqdm.notebook import tqdm as tqdm
import csv
from joblib import Parallel, delayed
import multiprocessing
# In[2]:
def get_pages(main):
try:
soup = connect(main)
n_pages = [_.get_text(strip=True) for _ in soup.find('ul', {'class': 'pagination pagination__number'}).find_all('li')]
#max = soup.find_all("span", class_="pagination__number")
last_page = int(n_pages[-1])
pages = [main]
for n in range(2,last_page+1):
page_num = "/?pag={}".format(n)
pages.append(main + page_num)
except:
pages = [main]
return pages
def connect(web_addr):
resp = requests.get(web_addr)
return BeautifulSoup(resp.content, "html.parser")
def get_areas(website):
data = connect(website)
areas = []
for ultag in data.find_all('ul', {'class': 'breadcrumb-list breadcrumb-list_list breadcrumb-list__related'}):
for litag in ultag.find_all('li'):
for i in range(len(litag.text.split(','))):
areas.append(litag.text.split(',')[i])
areas = [x.strip() for x in areas]
urls = []
for area in areas:
url = website + '/' + area.replace(' ','-').lower()
urls.append(url)
return areas, urls
def get_apartment_links(website):
data = connect(website)
links = []
for link in data.find_all('ul', {'class': 'annunci-list'}):
for litag in link.find_all('li'):
try:
#return litag.a.get('href')
links.append(litag.a.get('href'))
except:
continue
return links
#def scrape_link(website):
# data = connect(website)
# nomi = []
# valori = []
# temp = []
# for link in (data.find_all('dd', {'class': 'im-features__value'})[1], data.find_all('dd', {'class': 'im-features__value'})[9], data.find_all('dd', {'class': 'im-features__value'})[12]):
# temp.append(link)
# try:
# temp = str(temp).replace('</dd>', '')
# temp = str(temp).replace('<dt', '</dd><dt')
# temp = BeautifulSoup(temp, "html.parser")
# for elem in temp.find_all('dd'):
# valori.append(elem.string.strip())
# except:
# #print('errore')
# pass
# temp = []
#
# valori[-1] = (data.find_all('dd', {'class': 'im-features__value'})[0].find_all('span')[0].string)
#
# for link in data.find_all('dt'):
# if link.string == None:
# pass
# else:
# nomi.append(link.string.strip())
#
# while len(valori)<len(nomi):
# valori.append(0)
#
# while len(nomi)<len(valori):
# nomi.append('Prestazione energetica del fabbricato')
#
# count = 0
# nomi.append('Arredato S/N')
# nomi.remove('altre caratteristiche')
# for elem in data.find_all('dd', {'class': 'im-features__value'})[8].find_all('span'):
# if elem.string.strip() == "Arredato" or elem.string == "Non arredato" or elem.string == "Parzialmente arredato":
# valori.append(elem.string.strip())
# count = 1
# else:
# continue
# if count == 0:
# valori.append('0')
#
# valori = remove_duplicates(valori)
# nomi = remove_duplicates(nomi)
#
# return nomi, valori
def scrape_link(website):
data = connect(website)
info = data.find_all('dl', {'class': 'im-features__list'})
comp_info = pd.DataFrame()
cleaned_id_text = []
cleaned_id__attrb_text = []
for n in range(len(info)):
for i in info[n].find_all('dt'):
cleaned_id_text.append(i.text)
for i in info[n].find_all('dd'):
cleaned_id__attrb_text.append(i.text)
comp_info['Id'] = cleaned_id_text
comp_info['Attribute'] = cleaned_id__attrb_text
comp_info
feature = []
for item in comp_info['Attribute']:
try:
feature.append(clear_df(item))
except:
feature.append(ultra_clear_df(item))
comp_info['Attribute'] = feature
#comp_info.set_index('Id', drop=True, inplace=True)
#comp_info.index.name = None
#comp_info = comp_info.T.reset_index().drop(columns='index')
return comp_info['Id'].values, comp_info['Attribute'].values
def remove_duplicates(x):
return list(dict.fromkeys(x))
def clear_df(the_list):
the_list = (the_list.split('\n')[1].split(' '))
the_list = [value for value in the_list if value != ''][0]
return the_list
def ultra_clear_df(the_list):
the_list = (the_list.split('\n\n')[1].split(' '))
the_list = [value for value in the_list if value != ''][0]
the_list = (the_list.split('\n')[0])
return the_list
# In[3]:
## Link for city main website
## get areas inside the city (districts)
website = "https://www.immobiliare.it/affitto-case/torino"
areas, districts = get_areas(website)
print("Those are district's links \n")
print(districts)
# ## Scrape cycle initialization
# In[4]:
indirizzo = []
location = []
for url in tqdm(districts):
pages = get_pages(url)
for page in pages:
add = get_apartment_links(page)
indirizzo.append(add)
for num in range(0,len(add)):
location.append(url.rsplit('/', 1)[-1])
announces_links = [item for valore in indirizzo for item in valore]
# In[5]:
## Check that what you scraped has a meaning...and save it
print("The numerosity of announces:\n")
print(len(announces_links))
with open('announces_list.csv', 'w') as myfile:
wr = csv.writer(myfile)
wr.writerow(announces_links)
# ## Proper dataset creation by scraping every announce
# In[6]:
## DATAFRAME ORIZZONTALE__USARE
df_scrape = pd.DataFrame()
to_be_dropped = []
counter = 0
for link in tqdm(list(announces_links)):
counter=counter+1
try:
nomi, valori = scrape_link(link)
df_temporaneo = pd.DataFrame(columns=nomi)
df_temporaneo.loc[len(df_temporaneo), :] = valori[0:len(nomi)]
df_scrape = df_scrape.append(df_temporaneo, sort=False)
except Exception as e:
print(e)
to_be_dropped.append(counter)
print(to_be_dropped)
continue
# In[7]:
#for item in to_be_dropped:
pd.DataFrame(location).to_csv('location.csv', sep=';')
pd.DataFrame(to_be_dropped).to_csv('to_be_dropped.csv', sep=';')
# In[8]:
to_be_dropped.sort(reverse=True)
# In[9]:
for index in to_be_dropped:
del location[index-1]
# In[10]:
for index in to_be_dropped:
del announces_links[index-1]
# In[11]:
print(df_scrape.shape)
df_scrape['zona'] = location
df_scrape['links'] = announces_links
df_scrape.columns = map(str.lower, df_scrape.columns)
df_scrape.to_csv('dataset.csv', sep=";")
# In[12]:
df_scrape = pd.read_csv('dataset.csv', sep=';')
# In[13]:
df_scrape = df_scrape[['contratto', 'zona', 'tipologia', 'superficie', 'locali', 'piano', 'tipo proprietà', 'prezzo', 'spese condominio', 'spese aggiuntive', 'anno di costruzione', 'stato', 'riscaldamento', 'climatizzazione', 'classe energetica', 'posti auto', 'links']]
# In[14]:
def cleanup(df):
price = []
rooms = []
surface = []
bathrooms = []
floor = []
contract = []
tipo = []
condominio = []
heating = []
built_in = []
state = []
riscaldamento = []
cooling = []
energy_class = []
tipologia = []
pr_type = []
arredato = []
for tipo in df['tipologia']:
try:
tipologia.append(tipo)
except:
tipologia.append(None)
for superficie in df['superficie']:
try:
if "m" in superficie:
s = superficie.replace(" m²", "")
surface.append(s)
except:
surface.append(None)
for locali in df['locali']:
try:
rooms.append(locali[0:1])
except:
rooms.append(None)
for prezzo in df['prezzo']:
try:
price.append(prezzo.replace("Affitto ", "").replace("€ ", "").replace("/mese", "").replace(".",""))
except:
price.append(None)
for contratto in df['contratto']:
try:
contract.append(contratto.replace("\n ",""))
except:
contract.append(None)
for piano in df['piano']:
try:
floor.append(piano.split(' ')[0])
except:
floor.append(None)
for tipologia in df['tipo proprietà']:
try:
pr_type.append(tipologia.split(',')[0])
except:
pr_type.append(None)
for condo in df['spese condominio']:
try:
if "mese" in condo:
condominio.append(condo.replace("€ ","").replace("/mese",""))
else:
condominio.append(None)
except:
condominio.append(None)
for ii in df['spese aggiuntive']:
try:
if "anno" in ii:
mese = int(int(ii.replace("€ ","").replace("/anno","").replace(".",""))/12)
heating.append(mese)
else:
heating.append(None)
except:
heating.append(None)
for anno_costruzione in df['anno di costruzione']:
try:
built_in.append(anno_costruzione)
except:
built_in.append(None)
for stato in df['stato']:
try:
stat = stato.replace(" ","").lower()
state.append(stat)
except:
state.append(None)
for tipo_riscaldamento in df['riscaldamento']:
try:
if 'Centralizzato' in tipo_riscaldamento:
riscaldamento.append('centralizzato')
elif 'Autonomo' in tipo_riscaldamento:
riscaldamento.append('autonomo')
except:
riscaldamento.append(None)
for clima in df['climatizzazione']:
try:
cooling.append(clima.lower().split(',')[0])
except:
cooling.append('None')
for classe in df['classe energetica']:
try:
energy_class.append(classe[0:2])#.replace("\n ",""))
except:
energy_class.append(None)
#for SN in df['Arredato S/N']:
# try:
# arredato.append(SN)
# except:
# arredato.append(None)
#
final_df = pd.DataFrame(columns=['contratto', 'zona', 'tipologia', 'superficie', 'locali', 'piano', 'tipo proprietà', 'prezzo', 'spese condominio', 'spese aggiuntive', 'anno di costruzione', 'stato', 'riscaldamento', 'climatizzazione', 'certificazione energetica', 'posti auto'])#, 'Arredato S/N'])
final_df['contratto'] = contract
final_df['tipologia'] = tipologia
final_df['superficie'] = surface
final_df['locali'] = rooms
final_df['piano'] = floor
final_df['tipo proprietà'] = pr_type
final_df['prezzo'] = price
final_df['spese condominio'] = condominio
final_df['spese riscaldamento'] = heating
final_df['anno di costruzione'] = built_in
final_df['stato'] = state
final_df['riscaldamento'] = riscaldamento
final_df['climatizzatore'] = cooling
final_df['classe energetica'] = energy_class
final_df['zona'] = df['zona'].values
#inal_df['Arredato S/N'] = arredato
final_df['link annuncio'] = announces_links
return final_df
# In[15]:
final = cleanup(df_scrape)
final.to_csv('dataset_da_allenamento.csv', sep=";")
# In[ ]: