-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcrawler.py
357 lines (210 loc) · 9.53 KB
/
crawler.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
#given files named by house
#count how many has been sold
import requests # http request
from bs4 import BeautifulSoup as bs
import glob # list all files in a folder
import re
import json
import os.path
import pickle
userAgent = {"User-Agent": "Mozilla/4.0"}
def getPropertyList(requestUrl, n_page):
propertyList = []
for page in range(1, n_page+1):
print("Getting property list...%d/%d" % (page, n_page), end="\r")
soup = bs(requests.get(requestUrl + "/pg-" + str(page), headers=userAgent).text)
pagedList = soup.find("div", {"id": "ListView"}).findAll("div", {"class": "listing-enhanced"})
for propertyTag in pagedList:
propertyList.append("http://www.realtor.com" + propertyTag.find("a")['href'])
return propertyList
def getDescription(soup, data):
description = soup.find("p", {"class": "property-description"})
if description:
data["Description"] = description.text
def getGeneralInfo(soup, data):
generalInfo = soup.find("div", {"id": "GeneralInfo"})
if generalInfo:
for column in generalInfo.findAll("ul", {"class": "list-data"}):
for pair in column.findAll("li", {"class": "list-sidebyside"}):
keyValue = []
for content in pair.findAll("span"):
keyValue.append(content.text)
data[keyValue[0]] = keyValue[1]
def getMiscInfo(soup, data):
features = []
regex = re.compile("General Information|Listing Provider")
for feature in soup.find("div", {"id": "tab-overview"}).findAll("h3", {"class": "title-section"}):
if regex.search(feature.text) is None:
features.append(feature.text)
featureValues = []
for featureValue in soup.find("div", {"id": "tab-overview"}).findAll("ul", {"class": "list-disc"}):
values = []
for value in featureValue.findAll("li"):
values.append(value.text)
featureValues.append(values)
if len(features) == len(featureValues):
for i in range(len(features)):
data[features[i]] = featureValues[i]
else:
print("feature number and feature value number not match %d %d" % (len(features), len(featureValues)))
def getListingProvider(soup, data):
agent = soup.find("a", {"id": "agentNameLnk"})
if agent:
data["Listing Agent"] = agent.text
company = soup.find("th", text = re.compile("Listed by"))
if company:
data["Listing Company"] = company.parent.find("td").find("ul").find("li").text
def getSoldInfo(soup, data):
price = soup.find("div", {"id": "MetaData"}).find("span", {"class": "jumbo"})
if price:
data["Sold Price"] = price.text
date = soup.find("div", {"id": "MetaData"}).find("span", {"class": "sold-date"})
if date:
data["Sold Date"] = date.text
def getAssignedPublicSchools(soup, data):
schools = []
table = soup.find("div", {"id": "SchoolsAndNeighborhood"}).find("h2", text=re.compile("Assigned Public Schools"))
if table:
for entry in table.findNext("table", {"id": "mapDataTable"}).find("tbody").findAll("tr"):
school = {}
school["Name"] = entry.find("a").text
school["Distance"] = entry.find("td", text=re.compile("mi")).text
if entry.find("td", text=re.compile("-")):
school["Grade"] = entry.find("td", text=re.compile("-")).text
if entry.find("td", text=re.compile(":")):
school["Student/Teacher Ratio"] = entry.find("td", text=re.compile(":")).text
school["Rating"] = entry.find("i")["class"][0]
schools.append(school)
data["Assigned Public Schools"] = schools
def getMiddleSchools(soup, data):
schools = []
table = soup.find("div", {"id": "tab-school-middle"})
if table:
table = table.find("tbody")
if table:
for entry in table.findAll("tr"):
school = {}
school["Name"] = entry.find("a").text
school["Distance"] = entry.find("td", text=re.compile("mi")).text
if entry.find("td", text=re.compile("-")):
school["Grade"] = entry.find("td", text=re.compile("-")).text
if entry.find("td", text=re.compile(":")):
school["Student/Teacher Ratio"] = entry.find("td", text=re.compile(":")).text
school["Rating"] = entry.find("i")["class"][0]
schools.append(school)
data["Nearby Middle Schools"] = schools
def getElementarySchools(soup, data):
schools = []
table = soup.find("div", {"id": "tab-school-elementary"})
if table:
table = table.find("tbody")
if table:
for entry in table.findAll("tr"):
school = {}
school["Name"] = entry.find("a").text
school["Distance"] = entry.find("td", text=re.compile("mi")).text
if entry.find("td", text=re.compile("-")):
school["Grade"] = entry.find("td", text=re.compile("-")).text
if entry.find("td", text=re.compile(":")):
school["Student/Teacher Ratio"] = entry.find("td", text=re.compile(":")).text
school["Rating"] = entry.find("i")["class"][0]
schools.append(school)
data["Nearby Elementary Schools"] = schools
def getHighSchools(soup, data):
schools = []
table = soup.find("div", {"id": "tab-school-high"})
if table:
table = table.find("tbody")
if table:
for entry in table.findAll("tr"):
school = {}
school["Name"] = entry.find("a").text
school["Distance"] = entry.find("td", text=re.compile("mi")).text
if entry.find("td", text=re.compile("-")):
school["Grade"] = entry.find("td", text=re.compile("-")).text
if entry.find("td", text=re.compile(":")):
school["Student/Teacher Ratio"] = entry.find("td", text=re.compile(":")).text
school["Rating"] = entry.find("i")["class"][0]
schools.append(school)
data["Nearby High Schools"] = schools
def getPrivateSchools(soup, data):
schools = []
table = soup.find("div", {"id": "tab-school-private"})
if table:
table = table.find("tbody")
if table:
for entry in table.findAll("tr"):
school = {}
school["Name"] = entry.find("a").text
school["Distance"] = entry.find("td", text=re.compile("mi")).text
if entry.find("td", text=re.compile("-")):
school["Grade"] = entry.find("td", text=re.compile("-")).text
if entry.find("td", text=re.compile(":")):
school["Student/Teacher Ratio"] = entry.find("td", text=re.compile(":")).text
school["Rating"] = entry.find("i")["class"][0]
schools.append(school)
data["Nearby Private Schools"] = schools
def getPhotoUrls(html, data):
dataStrs = re.findall("\{\"ID\":\"PhotosContent\".+\"BatchSize\": \"21\"\}", html)
if len(dataStrs) > 0:
url = "http://www.realtor.com/public/PropertyDetail/PhotosContent?data=" + dataStrs[0]
contentType = {"content_type": "application/x-www-form-urlencoded"}
photoHtml = requests.post(url, headers=dict(list(contentType.items()) + list(userAgent.items()))).text
photoUrls = []
for photoUrl in re.findall("http://p.rdcpix.com/.+jpg", photoHtml):
photoUrls.append(re.sub("l\.jpg", "o.jpg", photoUrl))
data["Photo Urls"] = photoUrls
if __name__ == "__main__":
# Input parameters
n_page = 420
city = "San-Jose_CA"
folder = city + "_" + str(n_page)
listFile = folder + "/"+ folder
done = 0
# get all property urls
if os.path.exists(listFile): # check property url list file
infile = open(listFile, "r")
propertyList = [line.rstrip('\n') for line in infile]
infile.close()
else:
getPropertyListRequest = "http://www.realtor.com/propertyrecord-search/" + city + "/sby-10"
propertyList = getPropertyList(getPropertyListRequest, n_page)
# write property list to file
outfile = open(listFile, "w")
for s in propertyList:
outfile.write(s + '\n')
outfile.close()
for propertyUrl in propertyList:
done += 1
print("Getting property info...%d/%d" % (done, n_page*10), end="\r")
propertyName = re.sub("http://www.realtor.com/realestateandhomes-detail/|\?.+", "", propertyUrl)
outfilePath = folder + "/" + propertyName
# check file existence
if os.path.exists(outfilePath):
continue
# get html
html = requests.get(propertyUrl, headers=userAgent).text
soup = bs(html)
data = {}
# Description
getDescription(soup, data)
# General Info
getGeneralInfo(soup, data)
# Miscellaneous Info
getMiscInfo(soup, data)
# Listing Provider
getListingProvider(soup, data)
# Sold Info
getSoldInfo(soup, data)
# Nearby Schools
getAssignedPublicSchools(soup, data)
getElementarySchools(soup, data)
getMiddleSchools(soup, data)
getHighSchools(soup, data)
getPrivateSchools(soup, data)
# Photo Urls
getPhotoUrls(html, data)
# write file
outfile = open(outfilePath, "w")
json.dump(data, outfile)
outfile.close()