-
Notifications
You must be signed in to change notification settings - Fork 0
/
p.py
437 lines (350 loc) · 12 KB
/
p.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
436
437
#!/usr/bin/env python3
import sys
import os
import shutil
import requests
import threading
from requests.utils import requote_uri
from bs4 import BeautifulSoup
import random
import json
import re
import argparse
sys.setrecursionlimit(100000)
BASE_URL = 'https://www.che' + 'ck' + 'atr' + 'ad' + 'e.com'
PATH = 'temp'
URLS_PATH = PATH + '/urls.txt'
STATE_PATH = PATH + '/state.json'
CAT_PATH = 'categories.conf'
POST_PATH = 'postcodes.conf'
CAT_ALL_PATH = 'categories_all.conf'
POST_ALL_PATH = 'postcodes_all.conf'
RESULT = 'data.json'
HEADERS = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'}
def log(text, color = ''):
colors = {
'black': '\x1b[30m',
'red': '\x1b[31m',
'green': '\x1b[32m',
'yellow': '\x1b[33m',
'blue': '\x1b[34m',
'magenta': '\x1b[35m',
'cyan': '\x1b[36m',
'white': '\x1b[37m'
}
print(colors.get(color, '\x1b[0m'), text, '\x1b[0m')
##
# Let's get it started
##
os.system('clear')
parser = argparse.ArgumentParser()
parser.add_argument('-f', dest='f', action='store_const', const=True, help='Will start to parse from the ground erasing the previous parser state.')
args = parser.parse_args()
if args.f:
if os.path.exists(RESULT):
try:
os.remove(RESULT)
except OSError as e:
log('Cannot remove file %s %s' % (RESULT, e), 'red')
if os.path.exists(PATH):
try:
shutil.rmtree(PATH)
except OSError as e:
log('Cannot remove file %s %s' % (RESULT, e), 'red')
def getNodeText(node):
if node:
return node.text.replace('\n', ' ').strip()
else:
return ''
def getFirstNodeText(node):
if node:
return node[0].text.replace('\n', ' ').strip()
else:
return ''
def nrmlzMoney(s):
return re.sub('[£ ]+', '', s)
def decryptEmail(encodedString, url):
if not encodedString:
return ''
r = int(encodedString[:2],16)
email = ''.join([chr(int(encodedString[i:i+2], 16) ^ r) for i in range(2, len(encodedString), 2)])
return email
def makeUnique():
log('...Cleaning up duplicates...', 'magenta')
unique = []
with open(RESULT, 'r') as file:
data = json.loads(file.read())
for item in data:
if len(unique):
for i, u in enumerate(unique):
if item['name'] == u['name'] and item['email'] == u['email']:
break
else:
if i == len(unique) - 1:
unique.append(item)
else:
unique.append(item)
log('...DONE! %s duplicates were removed. %s companies are unique.' % (len(data) - len(unique), len(unique)), 'yellow')
with open(RESULT, 'w') as file:
file.write(json.dumps(unique))
# Save it just in case
with open('original_data.json', 'w') as file:
file.write(json.dumps(data))
def writeState(state):
if os.path.exists(STATE_PATH):
writeFile(state)
else:
if os.path.exists(PATH):
writeFile(state)
else:
# Create it initially
try:
os.mkdir(PATH)
os.chmod(PATH, 0o777)
writeFile(state)
except OSError as e:
log("Cannot create the directory '%s'. %s" % (PATH, e), 'red')
exit(-1)
def writeFile(state):
try:
with open(STATE_PATH, 'w') as file:
file.write(json.dumps(state))
except OSError as e:
log("Cannot write in file '%s'. %s" % (STATE_PATH, e), 'red')
exit(-1)
##
# Handle input or kinda
##
if os.path.exists(CAT_PATH):
with open(CAT_PATH, 'r') as file:
categories = file.readline().replace(' ', '').split(',')
else:
if os.path.exists(CAT_ALL_PATH):
with open(CAT_ALL_PATH, 'r') as file:
categories = file.readline().replace(' ', '').split(',')
else:
log('Cannot find %s / %s file' % (CAT_PATH, CAT_ALL_PATH), 'red')
exit(-1)
if os.path.exists(POST_PATH):
with open(POST_PATH, 'r') as file:
postcodes = file.readline().replace(' ', '').split(',')
else:
if os.path.exists(POST_ALL_PATH):
with open(POST_ALL_PATH, 'r') as file:
postcodes = file.readline().replace(' ', '').split(',')
else:
log('Cannot find %s / %s file' % (POST_PATH, POST_ALL_PATH), 'red')
exit(-1)
count = 0
totalPosts = len(postcodes) - 1
totalCats = len(categories) - 1
# Use previous values to resume the work
if os.path.exists(STATE_PATH):
print('%s does exist' % STATE_PATH)
with open(STATE_PATH, 'r') as file:
state = json.loads(file.read())
if state['cat'] == len(categories) - 1 and state['postcode'] == len(postcodes) - 1:
log('Seems like the previous task was completed. To run parser again add -f parameter to run.', 'yellow')
log('In this case ALL PARSED DATA WILL BE ERASED. So make sure you save/use data.', 'red')
exit()
else:
# Begin from the start with default values
state = {'page': 1, 'cat': 0, 'catName': '', 'postcode': 0}
writeState(state)
def checkNextSearch():
if not os.path.exists(URLS_PATH):
log('No %s file, so try to increase the page' % URLS_PATH, 'magenta')
state['page'] = state['page'] + 1
writeState(state)
getSearchResults()
startThreads()
def startThreads():
with open(URLS_PATH, 'r') as file:
lines = file.readlines()
linesCount = len(lines)
global results, threads
results = []
threads = {}
os.system('clear')
for i in range(linesCount):
url = requote_uri(lines[i]).rstrip('%0A')
thread = MultiThread(url, i)
thread.start()
threads[i] = thread
while threads:
pass
# when threads are completed
log('All threads are completed.', 'yellow')
res = ''
for i, item in enumerate(results):
# Avoid potenrially bad values
if not item:
continue
if os.path.exists(RESULT):
res += ',' + json.dumps(item)
else:
sep = '[' if i == 0 else ','
res += sep + json.dumps(item)
try:
with open(RESULT, 'a') as file:
file.write(res)
except OSError as e:
log("Cannot write in file '%s'. %s" % (RESULT, e), 'red')
exit(-1)
# Remove ulrs file
os.remove(URLS_PATH)
checkNextSearch()
def query():
return BASE_URL + '/Search/?postcode=%s&location=%s&page=%s&facet_Category=%s&secondary=true' % (postcodes[state['postcode']], postcodes[state['postcode']], state['page'], categories[state['cat']])
def getContent(url):
try:
response = requests.get(url, headers=HEADERS, timeout=180).text
except requests.RequestException as e:
if e.response is not None:
log(e.response + ' %s' % url, 'red')
else:
log(str(e) + ' %s' % url, 'red')
response = False
return response
def getSearchResults():
content = getContent(query())
if not content:
log('No response from server/ No search results', 'red')
# Try to get it again?
getSearchResults()
return
html = BeautifulSoup(content, 'html.parser')
searchRes = html.find(class_='results')
if None == searchRes or 1 == len(list(searchRes.children)): # 1 is element itself with no childrens
# Next query
log('No results for query %s' % query(), 'blue')
nextQuery()
return
links = searchRes.select('.results__title a')
state['catName'] = html.find('h1').text.split(' in ')[0]
with open(URLS_PATH, 'w') as file:
urls = ''
for i, link in enumerate(links):
urls += ('' if i == 0 else '\n') + BASE_URL + link.get('href')
file.write(urls)
def nextQuery():
state['page'] = 1
if len(postcodes) > state['postcode'] + 1:
state['postcode'] = state['postcode'] + 1
log('Next query. postcode: %s page: %s cat: %s' % (state['postcode'], state['page'], state['cat']), 'green')
writeState(state)
getSearchResults()
else:
if len(categories) > state['cat'] + 1:
state['cat'] = state['cat'] + 1
state['postcode'] = 0
log('Next query. postcode: %s page: %s cat: %s' % (state['postcode'], state['page'], state['cat']), 'green')
writeState(state)
getSearchResults()
else:
with open(RESULT, 'a') as file:
file.write(']')
log('...DONE!', 'yellow')
makeUnique()
exit()
def goTo(url, thread, color):
content = getContent(url)
if not content:
# maybe try to refresh the same URL?
log('No response from server. Trying to get response again', 'red')
goTo(url, thread, color)
return
return saveData(content, url, thread, color)
def saveData(content, url, thread, color):
html = BeautifulSoup(content, 'html.parser')
r = {}
name = getNodeText(html.find('h1'))
contact = html.find(class_='contact-card__contact-name')
phones = html.select('a[href^="tel"]')
phonesStr = ''
for i, phone in enumerate(phones):
phonesStr += phone.get('href')[4:]
if (i + 1 < len(phones)):
phonesStr += ', '
email = html.select('#ctl00_ctl00_content_ctlEmail span')
email = email[0].get('data-cfemail') if email else ''
email2 = html.select('#ctl00_ctl00_content_managedEmail span')
email2 = email2[0].get('data-cfemail') if email2 else ''
website = html.find(id='ctl00_ctl00_content_ctlWeb1')
score = html.find(class_='scores__overall-value')
reliability = html.select('.scores__row:nth-child(2) .scores__value')
tidiness = html.select('.scores__row:nth-child(3) .scores__value')
courtesy = html.select('.scores__row:nth-child(4) .scores__value')
workmanship = html.select('.scores__row:nth-child(5) .scores__value')
reviews = html.find(class_='page-nav__review-count')
basedIn = html.find(class_='address', recursive=True)
worksIn = html.select('#ctl00_ctl00_content_content_pnlWorksIn p')
r['name'] = name
r['pageURL'] = url
r['contact'] = getNodeText(contact)
r['phones'] = phonesStr
r['email'] = decryptEmail(email, url) if email else decryptEmail(email2, url)
r['website'] = website.get('href') if website else ''
r['score'] = getNodeText(score)
r['reliability'] = getFirstNodeText(reliability)
r['tidiness'] = getFirstNodeText(tidiness)
r['courtesy'] = getFirstNodeText(courtesy)
r['workmanship'] = getFirstNodeText(workmanship)
r['reviews'] = getNodeText(reviews)
r['basedin'] = getNodeText(basedIn)
r['worksin'] = getFirstNodeText(worksIn)
r['postcode'] = postcodes[state['postcode']]
r['category'] = categories[state['cat']]
r['categoryName'] = state['catName']
global count
count += 1
log('%s. %s postcode %s of %s category %s of %s page %s' % (count, thread, state['postcode'], totalPosts, state['cat'], totalCats, state['page']), color)
##
# getData2
##
content2 = getContent(url + 'Checks.aspx')
if not content2:
log('No response from page %s' % url + 'Checks.aspx', 'red')
content2 = getContent(url + 'Checks.aspx')
html2 = BeautifulSoup(content2, 'html.parser')
gasSafeNumber = html2.select('img[alt="Gas Safe Register"]')
gasSafeNumber = html2.find(class_='background-check__caption').text.replace('verify membership', '').replace('Get the Promise', '').strip() if gasSafeNumber else ''
companyType = html2.select('#ctl00_ctl00_content_content_trCompanyType td')
companyOwner = html2.select('#ctl00_ctl00_content_content_trOwners td')
companyLimitedCheked = html2.select('#ctl00_ctl00_content_content_trLimitedChecked td')
companyVAT = html2.find(id='ctl00_ctl00_content_content_lblVAT')
publicLiabilityInsurance = html2.find(id='ctl00_ctl00_content_content_lblInsurance')
insuredBy = html2.select('#ctl00_ctl00_content_content_pnlInsurance tr:nth-child(2) td')
insuranceAmount = html2.select('#ctl00_ctl00_content_content_pnlInsurance tr:nth-child(3) td')
r['gasSafeNumber'] = gasSafeNumber
r['companyType'] = getFirstNodeText(companyType)
r['companyOwner'] = getFirstNodeText(companyOwner)
r['companyLimitedCheked'] = getFirstNodeText(companyLimitedCheked)
r['companyVAT'] = getNodeText(companyVAT)
r['publicLiabilityInsurance'] = getNodeText(publicLiabilityInsurance)
r['insuredBy'] = getFirstNodeText(insuredBy)
r['insuranceAmount'] = nrmlzMoney(getFirstNodeText(insuranceAmount))
##
# return the data
##
return r
class MultiThread(threading.Thread):
def __init__(self, url, i):
threading.Thread.__init__(self)
self.i = i
self.url = url
self.lock = threading.Lock() # it seems like it works w/o lock, but for sure
self.setName('Thread #%s' % (self.i + 1))
self.color = random.choice(['magenta', 'cyan', 'yellow', 'green', 'blue'])
def run(self):
global results, threads
res = goTo(self.url, self.getName(), self.color)
with self.lock:
results.append(res)
del threads[self.i]
log('%s is finished.' % self.getName(), 'white')
if __name__ == '__main__':
if not os.path.exists(URLS_PATH):
log('No %s file, so go to search' % URLS_PATH, 'magenta')
getSearchResults()
startThreads()