-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfind_broken_links.py
291 lines (248 loc) · 8.76 KB
/
find_broken_links.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
print(
"Broken Link Finder\n"
"This script scans a web page of a given URL and validates the links on it\n"
"If the page has a link to the same hostname as the URL given by the user, its destination page also scanned\n"
"So potentially the whole site will be scanned (this may take hours!)\n"
"All broken links found are saved on broken-links-[date]-[time]-[random-ID].txt"
)
import datetime
import os
import queue
from concurrent.futures import ThreadPoolExecutor, as_completed
from random import randint
from urllib.parse import urlparse
import chromedriver_autoinstaller
import colorama
import requests
import urllib3
import validators
from selenium import webdriver
from selenium.webdriver.common.by import By
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
chromedriver_autoinstaller.install()
chrome_ua = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/"
"537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
)
}
req_kwargs = {"headers": chrome_ua, "timeout": 10, "verify": False}
def validate(link):
(url, text) = link
req_args = {"url": url} | req_kwargs
try:
r_head = requests.head(**req_args)
if r_head.ok:
return (True, r_head.status_code, url, text, "")
else:
try:
r_get = requests.get(**req_args)
return (r_get.ok, r_get.status_code, url, text, "")
except Exception as get_e:
return (False, 0, url, text, str(get_e))
except Exception:
try:
r_get = requests.get(**req_args)
return (r_get.ok, r_get.status_code, url, text, "")
except Exception as head_e:
return (False, 0, url, text, str(head_e))
# Colorama setup
colorama.init()
# URL setup
start_page = input("URL: ")
while not validators.url(start_page):
print("Invalid URL")
start_page = input("URL: ")
parsed_uri = urlparse(start_page)
hostname = "{uri.scheme}://{uri.netloc}/".format(uri=parsed_uri)
# Timer setup
start_time = datetime.datetime.now()
# Output file setup
datetime_string = start_time.strftime("%Y-%m-%d-%H-%M-%S")
script_path = os.path.dirname(os.path.abspath(__file__))
file_id = "".join(["%s" % randint(0, 9) for digit in range(0, 6)])
output_file_path = os.path.join(
script_path, "broken-links-" + datetime_string + "-" + file_id + ".txt"
)
output_file = open(output_file_path, "w", encoding="utf-8")
# We don't want to scan files (only web pages)
do_not_scan = (
"#",
".pdf",
".doc",
".docx",
".xls",
".xlsx",
".ppt",
".pptx",
".doc",
".docx",
".odt",
".ods",
".jpg",
".png",
".zip",
".rar",
)
# Also, we don't want to validate special links
do_not_validate = (
"javascript:",
"mailto:",
"tel:",
)
# Sets used to flag visited pages/requested URLs and avoid multiple scans/requests
scanned_pages = {start_page}
broken_urls = set()
ok_urls = set()
# Webdriver setup
options = webdriver.chrome.options.Options()
options.add_argument("--log-level=3") # minimal logging
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
driver.implicitly_wait = 1
# Find the broken links
output_file.write("Broken links found from " + start_page + "\n")
no_broken_links = True
page_counter = 1
page_total = 1
page_queue = queue.Queue()
page_queue.put(start_page)
while not page_queue.empty():
page = page_queue.get()
print("Scanning " + page + " (" + str(page_counter) + "/" + str(page_total) + ")")
page_counter = page_counter + 1
broken_link_found = False
try:
driver.get(page)
links_to_be_validated = set()
link_list = driver.find_elements(By.TAG_NAME, "a")
for link in link_list:
link_url = link.get_attribute("href")
link_text = link.text.strip()
# Check if link can or needed to be validated
if (
link_url
and link_url.strip()
and not link_url.startswith(do_not_validate)
and not link in ok_urls
):
# Check if the link has already found out to be broken (it will not be validated again)
if link_url in broken_urls:
if not broken_link_found:
broken_link_found = True
no_broken_links = False
output_file.write("\n" + page + "\n")
output_file.write(
"\t"
+ str(link_url)
+ " ("
+ link_text
+ "): Link found to be broken previously"
+ "\n"
)
print(
colorama.Fore.RED
+ "\t"
+ str(link_url)
+ " ("
+ link_text
+ "): Link found to be broken previously"
+ colorama.Style.RESET_ALL
)
# Link can be validated and is not known to be OK or broken, so put it on the list to be validated
else:
links_to_be_validated.add((link_url, link_text))
except Exception as err:
print(colorama.Fore.RED + "Could not scan " + page + colorama.Style.RESET_ALL)
print(str(err))
# Validate the links asynchronously
with ThreadPoolExecutor(max_workers=20) as executor:
req_futures = [
executor.submit(validate, requestable_link)
for requestable_link in links_to_be_validated
]
for req_future in as_completed(req_futures):
(
req_ok,
req_status_code,
req_url,
req_text,
req_exception_text,
) = req_future.result()
if req_ok:
ok_urls.add(req_url)
# If link has the same hostname as the start page AND has not been already scanned, add to scan queue
req_parsed_uri = urlparse(req_url)
req_hostname = "{uri.scheme}://{uri.netloc}/".format(uri=req_parsed_uri)
if (
req_hostname == hostname
and req_url not in scanned_pages
and not req_url.endswith(do_not_scan)
):
page_queue.put(req_url)
scanned_pages.add(req_url)
page_total = page_total + 1
print(
colorama.Fore.YELLOW
+ "\t"
+ req_url
+ " added to scan queue"
+ colorama.Style.RESET_ALL
)
# Broken link found
else:
if not broken_link_found:
broken_link_found = True
no_broken_links = False
output_file.write("\n" + page + "\n")
if req_exception_text == "":
print(
colorama.Fore.RED
+ "\t"
+ str(req_url)
+ " ("
+ req_text
+ "): "
+ str(req_status_code)
+ colorama.Style.RESET_ALL
)
output_file.write(
"\t"
+ str(req_url)
+ " ("
+ req_text
+ "): "
+ str(req_status_code)
+ "\n"
)
else:
print(
colorama.Fore.RED
+ "\t"
+ str(req_url)
+ " ("
+ req_text
+ "): "
+ req_exception_text
+ colorama.Style.RESET_ALL
)
output_file.write(
"\t"
+ str(req_url)
+ " ("
+ req_text
+ "): "
+ req_exception_text
+ "\n"
)
broken_urls.add(req_url)
driver.quit()
if no_broken_links:
output_file.seek(0)
output_file.truncate()
output_file.write("No broken links found from " + start_page + "\n")
running_time = datetime.datetime.now() - start_time
farewell_msg = "\n" + str(page_total) + " pages scanned in " + str(running_time)
print("Scan completed!" + farewell_msg)
output_file.write(farewell_msg)
output_file.close()