-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_scrapper_controllers.py
285 lines (256 loc) · 11.4 KB
/
web_scrapper_controllers.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
from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
import csv
import pandas as pd
from preprocessing_controllers import preprocess_document
excpt = []
def get_link_dictionary(web_links, url):
title_links = {}
for web_link in web_links:
if web_link.has_attr('title') and web_link.attrs['href'][0] != '#':
if web_link.attrs['href'].endswith('.php') and not url.endswith('.php'):
title_links[web_link.attrs['title']] = url + web_link.attrs['href']
else:
title_links[web_link.attrs['title']] = web_link.attrs['href']
elif web_link.has_attr('href'):
if web_link.attrs['href'][0] != '#':
if len(web_link.contents) == 1:
if web_link.attrs['href'].endswith('.php') and not url.endswith('.php'):
title_links[web_link.contents[0]] = url + web_link.attrs['href']
else:
title_links[web_link.contents[0]] = web_link.attrs['href']
elif (len(str(web_link.find('h4')).split('</')) >= 1) and (len(str(web_link.find('h4')).split('</')[0].split('>'))) >= 2:
ttl = str(web_link.find('h4')).split('</')[0].split('>')[1]
if web_link.attrs['href'].endswith('.php') and not url.endswith('.php'):
title_links[ttl] = url + web_link.attrs['href']
else:
title_links[ttl] = web_link.attrs['href']
else:
if web_link.attrs['href'].endswith('.php') and not url.endswith('.php'):
title_links[web_link] = url + web_link.attrs['href']
else:
title_links[web_link] = web_link.attrs['href']
else:
excpt.append(web_link)
return title_links
def get_link_dictionary_2(web_links, url, home_url='https://careers.humber.ca/'):
title_links = {}
for web_link in web_links:
try:
if web_link.has_attr('title') and web_link.attrs['href'][0] != '#':
if web_link.attrs['href'].endswith('.php'):
if url.endswith('/'):
title_links[web_link.attrs['title']] = url + web_link.attrs['href']
elif str(web_link.attrs['href']) not in str(url):
title_links[web_link.attrs['title']] = url + '/' + web_link.attrs['href']
else:
title_links[web_link.attrs['title']] = web_link.attrs['href']
elif web_link.has_attr('href'):
if web_link.attrs['href'][0] != '#':
if len(web_link.contents) == 1:
if web_link.attrs['href'].endswith('.php') and not url.endswith('.php'):
if url.endswith('/'):
title_links[web_link.contents[0]] = url + web_link.attrs['href']
elif str(web_link.attrs['href']) not in str(url):
title_links[web_link.contents[0]] = url + '/' + web_link.attrs['href']
else:
if web_link.contents[0] in title_links.keys():
if web_link.attrs['href'].endswith('.php'):
title_links[web_link.contents[0] + web_link.attrs['href']] = home_url + web_link.attrs['href']
else:
title_links[web_link.contents[0] + web_link.attrs['href']] = web_link.attrs['href']
else:
if web_link.attrs['href'].endswith('.php'):
title_links[web_link.contents[0]] = home_url + web_link.attrs['href']
else:
title_links[web_link.contents[0]] = web_link.attrs['href']
elif (len(str(web_link.find('h4')).split('</')) >= 1) and (len(str(web_link.find('h4')).split('</')[0].split('>'))) >= 2:
ttl = str(web_link.find('h4')).split('</')[0].split('>')[1]
if web_link.attrs['href'].endswith('.php') and not url.endswith('.php'):
if url.endswith('/'):
title_links[ttl] = url + web_link.attrs['href']
elif str(web_link.attrs['href']) not in str(url):
title_links[ttl] = url + '/' + web_link.attrs['href']
else:
title_links[ttl] = web_link.attrs['href']
else:
if web_link.attrs['href'].endswith('.php') and not url.endswith('.php'):
if url.endswith('/'):
title_links[web_link] = url + web_link.attrs['href']
elif str(web_link.attrs['href']) not in str(url):
title_links[web_link] = url + '/' + web_link.attrs['href']
else:
title_links[web_link] = web_link.attrs['href']
else:
excpt.append(web_link)
except:
continue
return title_links
def get_web_links(url):
try:
page = urlopen(url)
html = page.read()
soup = BeautifulSoup(html, "html.parser")
web_links = soup.select('a')
return web_links
except:
return []
def link_to_purpose_mapping(link_dictionary):
link_to_purpose = {}
for key in link_dictionary.keys():
value = link_dictionary[key]
if value not in link_to_purpose.keys():
link_to_purpose[value] = [key]
else:
link_to_purpose[value].append(key)
return link_to_purpose
def split_and_form_url(joined_urls):
urls_now = []
if 'https' in joined_urls:
splitted = joined_urls.split('https')
for i in splitted:
if len(i) > 0:
urls_now.append('https' + i)
else:
urls_now.append(joined_urls)
return urls_now
def add_new_to_old(old_dict, new_dict, url_chkd, last_part, depth):
new_urls = []
for i in new_dict.keys():
if '//' in i and '/' in i.split('//')[1]:
add = i.split('//')[1].split('/')[0]
if len(i) < 100 and ('careers.humber' in add or 'devant' in add):
if i not in old_dict.keys():
if i not in url_chkd and i.split('/')[-1] not in last_part and len(i.split('.php')) < depth:
new_urls += split_and_form_url(i)
old_dict[i] = new_dict[i]
else:
old_dict[i] = old_dict[i] + new_dict[i]
unique_purpose = list(dict.fromkeys(old_dict[i]))
old_dict[i] = unique_purpose
return old_dict, new_urls
def all_paths(url, depth):
link_to_purpose = {}
link_to_purpose[url] = ['Home Page']
link_to_purpose
url_checked = []
last_part = []
last_part.append('index.php')
web_pages = []
urls = list(link_to_purpose.keys())
while(len(urls) != 0):
new_urls = []
i = urls[0]
urls.remove(i)
print(i)
if i not in url_checked:
wb_lnk = get_web_links(i)
url_checked.append(i)
if len(wb_lnk) > 0:
lnk_dict = get_link_dictionary_2(wb_lnk, i)
lnk_purps = link_to_purpose_mapping(lnk_dict)
link_to_purpose, new_urls = add_new_to_old(link_to_purpose, lnk_purps, url_checked, last_part, depth)
urls = list(set(urls + new_urls))
return link_to_purpose
def limited_paths(url):
link_to_purpose = {}
link_to_purpose[url] = ['Home Page']
link_to_purpose
url_checked = []
web_pages = []
new_urls = list(link_to_purpose.keys())
urls = []
while(len(new_urls) != 0):
urls = new_urls
new_urls = []
for i in urls:
print(i)
if i not in url_checked:
wb_lnk = get_web_links(i)
url_checked.append(i)
if len(wb_lnk) > 0:
lnk_dict = get_link_dictionary(wb_lnk, i)
lnk_purps = link_to_purpose_mapping(lnk_dict)
link_to_purpose, new_urls = add_new_to_old(link_to_purpose, lnk_purps, url_checked)
return link_to_purpose
def get_web_document(url):
try:
page = urlopen(url)
html = page.read()
soup = BeautifulSoup(html, "html.parser")
web_doc = soup.get_text()
return re.sub(r'\n\n+', '', str(web_doc))
except:
return ''
def get_text_as_doc_from_websites(all_url):
link_doc_dict = {'Link':'Document'}
doc_link_dict = {'Document': ['Link']}
lnk_dc = {'Link':'Document'}
all_doc = []
exception_links = []
for i in set(all_url):
doc = get_web_document(i)
if len(doc) > 0:
lnk_dc[i] = doc
if len(doc) != 0 and doc not in all_doc:
link_doc_dict[i] = doc
all_doc.append(doc)
if len(doc) != 0 and doc not in doc_link_dict.keys():
doc_link_dict[doc] = [i]
elif len(doc) != 0:
doc_link_dict[doc].append(i)
else:
exception_links.append(i)
return link_doc_dict, doc_link_dict, all_doc, exception_links, lnk_dc
def get_all_urls(all_urls):
unique_urls = []
for i in all_urls:
if len(i.split('/')) < 5:
unique_urls.append(i)
return unique_urls
def generate_document_from_scraped_data(csv_file):
dct_links = pd.read_csv(csv_file)
home_url = ['https://careers.humber.ca/']
links_other = dct_links['https://careers.humber.ca/']
all_url = home_url + list(links_other)
unique_urls = get_all_urls(all_url)
lnk_dc_dct, dc_lnk_dct, all_dc, ex_lnks, lnk_dc = get_text_as_doc_from_websites(unique_urls)
del lnk_dc['Link']
link_doc_df = pd.DataFrame(lnk_dc_dct.items())
doc_link_df = pd.DataFrame(dc_lnk_dct.items())
link_document_df = pd.DataFrame(lnk_dc.items())
link_doc_df.to_excel('Link_to_doc.xlsx', engine='xlsxwriter')
doc_link_df.to_excel('Doc_to_link.xlsx', engine='xlsxwriter')
link_document_df.to_excel('Link_Document_Complete_Processed.xlsx', engine='xlsxwriter')
return './Link_Document_Complete_Processed.xlsx'
def web_scrapper(root):
link_to_purpose = all_paths(root, 3)
all_one_val = {}
for k,v in link_to_purpose.items():
for val in v:
if k not in all_one_val.keys():
all_one_val[k] = str(val)
else:
all_one_val[k] = all_one_val[k] + ', ' + str(val)
long_dict, short_dict = {}, {}
for k,v in all_one_val.items():
if len(v) > 200:
long_dict[k] = v
else:
short_dict[k] = v
all_link_purpose = {}
for k,v in short_dict.items():
all_link_purpose[k]=v
for k,v in long_dict.items():
all_link_purpose[k]=v
with open('link-dict-2.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in all_link_purpose.items():
writer.writerow([key, value])
with open('dict-2.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in link_to_purpose.items():
writer.writerow([key, value])
url_to_document = generate_document_from_scraped_data('dict-2.csv')
return url_to_document