-
Notifications
You must be signed in to change notification settings - Fork 24
/
immoscraper.py
256 lines (155 loc) · 7.04 KB
/
immoscraper.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
#!/usr/bin/env python#!/usr/bin/python
# coding: utf-8
# # Immoscout24.de Scraper
#
# Ein Script zum dumpen (in `.csv` schreiben) von Immobilien, welche auf [immoscout24.de](http://immoscout24.de) angeboten werden
# In[1]:
from bs4 import BeautifulSoup
import json
import urllib.request as urllib2
import random
from random import choice
import time
# In[2]:
# urlquery from Achim Tack. Thank you!
# https://github.com/ATack/GoogleTrafficParser/blob/master/google_traffic_parser.py
def urlquery(url):
# function cycles randomly through different user agents and time intervals to simulate more natural queries
try:
sleeptime = float(random.randint(1,6))/5
time.sleep(sleeptime)
agents = ['Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17',
'Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0',
'Opera/12.80 (Windows NT 5.1; U; en) Presto/2.10.289 Version/12.02',
'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
'Mozilla/3.0',
'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3',
'Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3',
'Opera/9.00 (Windows NT 5.1; U; en)']
agent = choice(agents)
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', agent)]
html = opener.open(url).read()
time.sleep(sleeptime)
return html
except Exception as e:
print('Something went wrong with Crawling:\n%s' % e)
# In[3]:
def immoscout24parser(url):
''' Parser holt aus Immoscout24.de Suchergebnisseiten die Immobilien '''
try:
soup = BeautifulSoup(urlquery(url), 'html.parser')
scripts = soup.findAll('script')
for script in scripts:
#print script.text.strip()
if 'IS24.resultList' in script.text.strip():
s = script.string.split('\n')
for line in s:
#print('\n\n\'%s\'' % line)
if line.strip().startswith('resultListModel'):
resultListModel = line.strip('resultListModel: ')
immo_json = json.loads(resultListModel[:-1])
searchResponseModel = immo_json[u'searchResponseModel']
resultlist_json = searchResponseModel[u'resultlist.resultlist']
return resultlist_json
except Exception as e:
print("Fehler in immoscout24 parser: %s" % e)
# ## Main Loop
#
# Geht Wohnungen und Häuser, jeweils zum Kauf und Miete durch und sammelt die Daten
# In[4]:
immos = {}
# See immoscout24.de URL in Browser!
b = 'Sachsen' # Bundesland
s = 'Dresden' # Stadt
k = 'Wohnung' # Wohnung oder Haus
w = 'Kauf' # Miete oder Kauf
page = 0
print('Suche %s / %s' % (k, w))
while True:
page+=1
url = 'http://www.immobilienscout24.de/Suche/S-T/P-%s/%s-%s/%s/%s?pagerReporting=true' % (page, k, w, b, s)
# Because of some timeout or immoscout24.de errors,
# we try until it works \o/
resultlist_json = None
while resultlist_json is None:
try:
resultlist_json = immoscout24parser(url)
numberOfPages = int(resultlist_json[u'paging'][u'numberOfPages'])
pageNumber = int(resultlist_json[u'paging'][u'pageNumber'])
except:
pass
if page>numberOfPages:
break
# Get the data
for resultlistEntry in resultlist_json['resultlistEntries'][0][u'resultlistEntry']:
realEstate_json = resultlistEntry[u'resultlist.realEstate']
realEstate = {}
realEstate[u'Miete/Kauf'] = w
realEstate[u'Haus/Wohnung'] = k
realEstate['address'] = realEstate_json['address']['description']['text']
realEstate['city'] = realEstate_json['address']['city']
realEstate['postcode'] = realEstate_json['address']['postcode']
realEstate['quarter'] = realEstate_json['address']['quarter']
try:
realEstate['lat'] = realEstate_json['address'][u'wgs84Coordinate']['latitude']
realEstate['lon'] = realEstate_json['address'][u'wgs84Coordinate']['longitude']
except:
realEstate['lat'] = None
realEstate['lon'] = None
realEstate['title'] = realEstate_json['title']
realEstate['numberOfRooms'] = realEstate_json['numberOfRooms']
realEstate['livingSpace'] = realEstate_json['livingSpace']
if k=='Wohnung':
realEstate['balcony'] = realEstate_json['balcony']
realEstate['builtInKitchen'] = realEstate_json['builtInKitchen']
realEstate['garden'] = realEstate_json['garden']
realEstate['price'] = realEstate_json['price']['value']
realEstate['privateOffer'] = realEstate_json['privateOffer']
elif k=='Haus':
realEstate['isBarrierFree'] = realEstate_json['isBarrierFree']
realEstate['cellar'] = realEstate_json['cellar']
realEstate['plotArea'] = realEstate_json['plotArea']
realEstate['price'] = realEstate_json['price']['value']
realEstate['privateOffer'] = realEstate_json['privateOffer']
realEstate['energyPerformanceCertificate'] = realEstate_json['energyPerformanceCertificate']
realEstate['floorplan'] = realEstate_json['floorplan']
realEstate['from'] = realEstate_json['companyWideCustomerId']
realEstate['ID'] = realEstate_json[u'@id']
realEstate['url'] = u'https://www.immobilienscout24.de/expose/%s' % realEstate['ID']
immos[realEstate['ID']] = realEstate
print('Scrape Page %i/%i (%i Immobilien %s %s gefunden)' % (page, numberOfPages, len(immos), k, w))
# In[ ]:
# In[ ]:
# In[6]:
print("Scraped %i Immos" % len(immos))
# ## Datenaufbereitung & Cleaning
#
# Die gesammelten Daten werden in ein sauberes Datenformat konvertiert, welches z.B. auch mit Excel gelesen werden kann. Weiterhin werden die Ergebnisse pseudonymisiert, d.h. die Anbieter bekommen eindeutige Nummern statt Klarnamen.
# In[7]:
from datetime import datetime
timestamp = datetime.strftime(datetime.now(), '%Y-%m-%d')
# In[8]:
import pandas as pd
# In[9]:
df = pd.DataFrame(immos).T
df.index.name = 'ID'
# In[ ]:
# In[10]:
df.livingSpace[df.livingSpace==0] = None
df['EUR/qm'] = df.price / df.livingSpace
# In[11]:
df.sort_values(by='EUR/qm', inplace=True)
# In[12]:
len(df)
# In[13]:
df.head()
# ## Alles Dumpen
# In[14]:
f = open('%s-%s-%s-%s-%s.csv' % (timestamp, b, s, k, w), 'w')
f.write('# %s %s from immoscout24.de on %s\n' % (k,w,timestamp))
df[(df['Haus/Wohnung']==k) & (df['Miete/Kauf']==w)].to_csv(f, encoding='utf-8')
f.close()
# In[15]:
df.to_excel('%s-%s-%s-%s-%s.xlsx' % (timestamp, b, s, k, w))
# Fragen? [@Balzer82](https://twitter.com/Balzer82)