-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_links.py
31 lines (25 loc) · 869 Bytes
/
check_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
import urllib.request
import re
def load_file(path):
with open(path, 'r') as file:
data = file.read().replace('\n', '')
return data
if __name__ == '__main__':
text = load_file('README.md')
s = re.findall('\((.*?)\)',text)
broken_links = list()
for link in s:
try:
conn = urllib.request.urlopen(link)
except urllib.error.HTTPError as e:
print('HTTPError: {}'.format(e.code) + ', ' + link)
broken_links.append(link)
except urllib.error.URLError as e:
print('URLError: {}'.format(e.reason) + ', ' + link)
except ValueError as e:
print('ValueError: {}'.format(e) + ', ' + link)
else:
print('good' + ', ' + link)
with open('broken_links.txt', 'w') as f:
for line in broken_links:
f.write(f"{line}\n")