-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparseremaxurlsdata_mt.py
327 lines (274 loc) · 9.54 KB
/
parseremaxurlsdata_mt.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
#!/usr/bin/env python
# -*- coding: latin-1 -*-
from bs4 import BeautifulSoup
import mechanize
import cookielib
from copy import copy
from time import sleep
import json
import pdb
import re
from time import time
from Queue import Queue, Empty
from threading import Thread
from threading import Lock
import codecs
import urlparse
STARTTIME=time()
COUNT=0
LASTSAVE=time()
MAXSAVEINTERVAL=60 # seconds
JSONDATA={}
URLPREFIX = 'http://www.remax.pt'
TIPOSPROPRIEDADE=['Apartamento', 'Moradia/Vivenda', 'Duplex']
inputdir='output/json/'
outputdir='output/json/'
jsonfile='mined_urls_arr.json'
def getspecimenlinks(soup):
"""Returns a dictionary[id]=href containing the captured specimend links"""
links={}
for td in soup.find_all('td', 'proplist_id'):
links[td.a.string]=td.a['href']
return links
def loadjson(jsonfile):
emptyjson={}
try:
loadedfile=file(jsonfile, 'r')
filetostring=loadedfile.read().decode("utf-8-sig")
result=json.loads(filetostring)
return result
except IOError as e:
print 'Ficheito %s vazio' % jsonfile
return emptyjson
except Exception as e:
print e
#pdb.set_trace()
def savedata(JSONDATA, jsonfile):
#jsonvar=loadjson(jsonfile) #Load existing json
#try:
# jsonvar[id]=item
#except Exception as e:
# print e
#pdb.set_trace()
with codecs.open(jsonfile, 'w', 'utf-8') as output_file:
#try:
json.dump(JSONDATA, output_file, indent=4, sort_keys=False, ensure_ascii=False)
#except Exception as e:
# print e
# print 'Jsonvar:'
# print jsonvar
# print 'Json inserido: '
# print item
# pdb.set_trace()
def idexists(id, jsonfile):
id=str(id)
jsonvar=loadjson(jsonfile) #Load existing json
if id in jsonvar:
return True
else:
return False
def newconfbrowser():
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=10)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
return br,cj
def parsetabledata(sopa):
caract={}
for div in sopa.find_all('div', 'data-item'):
#pdb.set_trace()
value=div.contents[1].text
field=div.contents[2].text
#pdb.set_trace()
if all([field, value]):
caract[field]=value
else:
pdb.set_trace()
print 'Value'+str(value)
print 'Field'+str(field)
print 'Erro parsing field'
return caract
def extractfloat(string):
return re.findall("[-+]?\d*\.\d+|\d+", string)[0] # re.findall returns a list
def parsegpscoords(soup):
scripts=soup.find_all(has_no_src)
lat=None
lng=None
for js in scripts:
jstext=js.text
for l in jstext.splitlines():
if l.find(' var lat') >0:
lat=extractfloat(l)
if l.find(' var lng') >0:
lng=extractfloat(l)
if all([lat, lng]):
return lat, lng
else:
return False
def has_no_src(tag):
return tag.name=='script' and not tag.has_attr('src') and not tag.has_attr('type')
class ThreadMechanizeUrl(Thread):
def __init__ (self, outputfile, queue, lock):
self.outputfile = outputfile
self.queue=queue
self.lock=lock
Thread.__init__ (self)
def run(self):
global STARTTIME
global COUNT
global JSONDATA
global LASTSAVE
global URLPREFIX
while True:
(id, data)=self.queue.get()
if 'tipo_propriedade' in data and data['tipo_propriedade'].strip() not in TIPOSPROPRIEDADE:
print('Not needed. Skipping to next...')
self.queue.task_done()
continue
elapsed=time()-STARTTIME
parspersec=COUNT/elapsed
# data={}
print "%s urls in %0.2f min " % (COUNT, elapsed/60.0) + " | %0.2furls/sec | " % parspersec +'Parsing:'+id
(br, cj)=newconfbrowser()
try:
url=URLPREFIX+data['url']
html=br.open(url).read()
#pdb.set_trace()
except:
print url+'!! UNAVAILABLE !!'
self.queue.task_done()
continue
else:
soup=BeautifulSoup(html)
# Default values
data['url']=url
data['header']=""
data['titulo']=""
data['preco']=""
data['properties']=""
data['desc']=""
data['erate']=""
data['lat']=""
data['lng']=""
data['address']=""
data['agentname']=""
data['agentid']=""
data['agentaddress']=""
data['features']=[]
cabecalho=soup.find(id='ctl00_h1PropertyDetailsTitle')
title=soup.find(id='ctl00_lblListingTitle')
price=soup.find(attrs={"class":"key-price"})
properties=parsetabledata(soup)
desc=soup.find(attrs={"class":'desc-long'})
erating=soup.find(attrs={"id":'ctl02_imgEnergyRating'})
address=soup.find(attrs={"class":"key-address-td"})
agentcard=soup.find(attrs={"class":"agentcard-main"})
features=soup.find_all(class_="feature-item")
try:
data['header']=cabecalho.text
except:
print 'Error parsing header of '+id
pass
try:
data['titulo']=title.text
except:
print 'Error parsing title of '+id
pass
try:
data['preco']=price.text
except:
print 'Error parsing price of '+id
pass
try:
data['properties']=properties
except:
print 'Error parsing properties of '+id
pass
try:
data['desc']=desc.text
except:
print 'Error parsing description of '+id
pass
try:
classif=erating['src'].split('_')[1].split('.')[0]
data['erate']=classif
except:
print 'Error parsing rate of '+id
pass
try:
data['address']=address.next_element.strip()
except:
print 'Error parsing address of '+id
pass
try:
data['agentname']=agentcard.h3.text
except:
print 'Error parsing agentname of '+id
pass
try:
agenthref=agentcard.h3.a['href']
parsedurl=urlparse.urlparse(agenthref)
getvars=urlparse.parse_qs(parsedurl.query)
data['agentid']=getvars['AgentID'][0]
except:
print 'Error parsing agentid of '+id
pass
try:
agentaddress=agentcard.find(attrs={"class":"agentcard-address"})
data['agentaddress']=u' '.join( [unicode(navstring).strip() for navstring in agentaddress.strings] )
except:
print 'Error parsing agentaddress of '+id
pass
try:
for feature in features:
data['features'].append(feature.text)
except:
print 'Error parsing features of '+id
pass
try:
gps=parsegpscoords(soup)
if gps:
(data['lat'], data['lng'])=gps
except:
print 'Error parsing coords of '+id
pass
# Adds data
JSONDATA[id]=data
if (time()-LASTSAVE) > MAXSAVEINTERVAL:
self.lock.acquire()
savedata(copy(JSONDATA), outputjsonfile)
self.lock.release()
LASTSAVE=time()
print "Data saved..."
COUNT+=1
self.queue.task_done()
self.lock.acquire()
savedata(copy(JSONDATA), outputjsonfile)
self.lock.release()
print "End of task"
print 'Started...'
queue = Queue()
lock = Lock()
outputjsonfile=outputdir+jsonfile.strip(".json")+'_out.json'
urls=loadjson(inputdir+jsonfile)
JSONDATA=loadjson(outputjsonfile)
#spawn a pool of threads, and pass them queue instance
for i in range(5):
t = ThreadMechanizeUrl(outputjsonfile, queue, lock)
t.setDaemon(True)
t.start()
for id, value in urls.items():
if id in JSONDATA:
print id+' already parsed. Continuing to the next one...'
else:
try:
queue.put((id, value))
#pdb.set_trace()
except KeyboardInterrupt:
print "Stopping..."
queue.join()
savedata(copy(JSONDATA), outputjsonfile)