This repository was archived by the owner on Apr 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathcheck_dead_links.py
More file actions
277 lines (229 loc) · 8.93 KB
/
check_dead_links.py
File metadata and controls
277 lines (229 loc) · 8.93 KB
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
#!/usr/bin/env python3
"""Dead link checker for the Reflex website.
Crawls the deployed site and checks for broken links.
"""
import argparse
import sys
import time
import xml.etree.ElementTree as ET
from collections import deque
from urllib.parse import urljoin, urlparse
import requests
from bs4 import BeautifulSoup
class DeadLinkChecker:
def __init__(self, base_url, max_pages=None, timeout=10, delay=0.5):
self.base_url = base_url.rstrip("/")
self.domain = urlparse(base_url).netloc
self.max_pages = max_pages
self.timeout = timeout
self.delay = delay
self.visited_pages = set()
self.checked_links = set()
self.dead_links = []
self.pages_to_visit = deque([base_url])
self.session = requests.Session()
self.session.headers.update(
{"User-Agent": "Mozilla/5.0 (compatible; DeadLinkChecker/1.0)"}
)
def is_internal_url(self, url):
"""Check if URL is internal to our domain."""
parsed = urlparse(url)
return parsed.netloc == self.domain or parsed.netloc == ""
def normalize_url(self, url):
"""Normalize URL for comparison."""
parsed = urlparse(url)
normalized = f"{parsed.scheme}://{parsed.netloc}{parsed.path}"
if parsed.query:
normalized += f"?{parsed.query}"
return normalized
def is_image_url(self, url):
"""Check if URL points to an image file."""
image_extensions = {
".webp",
".png",
".jpg",
".jpeg",
".gif",
".svg",
".ico",
".bmp",
".tiff",
}
parsed = urlparse(url)
path_lower = parsed.path.lower()
return any(path_lower.endswith(ext) for ext in image_extensions)
def check_link(self, url, source_page):
"""Check if a single link is working."""
if url in self.checked_links:
return True
self.checked_links.add(url)
parsed = urlparse(url)
if parsed.netloc in ["fonts.googleapis.com", "fonts.gstatic.com"]:
return True
try:
response = self.session.head(
url, timeout=self.timeout, allow_redirects=True
)
if response.status_code == 405:
response = self.session.get(
url, timeout=self.timeout, allow_redirects=True
)
if response.status_code == 403 and parsed.netloc in [
"twitter.com",
"www.twitter.com",
"x.com",
"www.x.com",
]:
print(f"Warning: Twitter/X link may be blocked by bot detection: {url}")
return True
if response.status_code >= 400:
self.dead_links.append(
{
"url": url,
"status_code": response.status_code,
"source_page": source_page,
"error": f"HTTP {response.status_code}",
}
)
return False
except requests.exceptions.RequestException as e:
self.dead_links.append(
{
"url": url,
"status_code": None,
"source_page": source_page,
"error": str(e),
}
)
return False
return True
def extract_links(self, html, page_url):
"""Extract all links from HTML content."""
soup = BeautifulSoup(html, "html.parser")
links = []
for tag in soup.find_all(["a", "link", "img", "script"]):
url = None
if tag.name == "a" or tag.name == "link":
url = tag.get("href")
elif tag.name == "img" or tag.name == "script":
url = tag.get("src")
if url:
absolute_url = urljoin(page_url, url)
if not absolute_url.startswith(
("javascript:", "mailto:", "tel:", "data:")
):
links.append(absolute_url)
return links
def crawl_page(self, url):
"""Crawl a single page and extract links."""
if url in self.visited_pages or (
self.max_pages and len(self.visited_pages) >= self.max_pages
):
return []
self.visited_pages.add(url)
print(f"Crawling: {url}")
try:
response = self.session.get(url, timeout=self.timeout)
response.raise_for_status()
content_type = response.headers.get("content-type", "").lower()
if "text/html" not in content_type:
return []
links = self.extract_links(response.text, url)
for link in links:
# Only check internal links and skip images
if self.is_internal_url(link) and not self.is_image_url(link):
self.check_link(link, url)
normalized = self.normalize_url(link)
if normalized not in self.visited_pages:
self.pages_to_visit.append(normalized)
time.sleep(self.delay)
return links
except requests.exceptions.RequestException as e:
print(f"Error crawling {url}: {e}")
return []
def get_sitemap_urls(self):
"""Try to get URLs from sitemap.xml."""
sitemap_url = f"{self.base_url}/sitemap.xml"
print(f"Checking for sitemap at: {sitemap_url}")
try:
response = self.session.get(sitemap_url, timeout=self.timeout)
if response.status_code == 200:
print("✅ Found sitemap.xml, parsing URLs...")
root = ET.fromstring(response.content)
urls = []
for url_elem in root.findall(
".//{https://www.sitemaps.org/schemas/sitemap/0.9}url"
):
loc_elem = url_elem.find(
"{https://www.sitemaps.org/schemas/sitemap/0.9}loc"
)
if loc_elem is not None and loc_elem.text:
urls.append(loc_elem.text)
if not urls:
for url_elem in root.findall(".//url"):
loc_elem = url_elem.find("loc")
if loc_elem is not None and loc_elem.text:
urls.append(loc_elem.text)
print(f"Found {len(urls)} URLs in sitemap")
return urls if urls else None
else:
print(f"No sitemap found (HTTP {response.status_code})")
return None
except Exception as e:
print(f"Error fetching sitemap: {e}")
return None
def run(self):
"""Run the dead link checker."""
print(f"Starting dead link check for {self.base_url}")
print(f"Max pages: {self.max_pages}, Timeout: {self.timeout}s")
sitemap_urls = self.get_sitemap_urls()
if sitemap_urls:
print("Using sitemap-based crawling...")
for url in sitemap_urls:
if not self.max_pages or len(self.visited_pages) < self.max_pages:
self.crawl_page(url)
else:
print("Using breadth-first crawling...")
while self.pages_to_visit and (
not self.max_pages or len(self.visited_pages) < self.max_pages
):
url = self.pages_to_visit.popleft()
self.crawl_page(url)
print("\nCrawl complete!")
print(f"Pages visited: {len(self.visited_pages)}")
print(f"Links checked: {len(self.checked_links)}")
print(f"Dead links found: {len(self.dead_links)}")
if self.dead_links:
print("\n❌ DEAD LINKS FOUND:")
for link_info in self.dead_links:
print(f" URL: {link_info['url']}")
print(f" Error: {link_info['error']}")
print(f" Found on: {link_info['source_page']}")
print()
return False
else:
print("\n✅ No dead links found!")
return True
def main():
parser = argparse.ArgumentParser(description="Check for dead links on a website")
parser.add_argument("url", help="Base URL to start crawling from")
parser.add_argument(
"--max-pages", type=int, default=500, help="Maximum pages to crawl"
)
parser.add_argument(
"--timeout", type=int, default=10, help="Request timeout in seconds"
)
parser.add_argument(
"--delay", type=float, default=0.5, help="Delay between requests"
)
args = parser.parse_args()
checker = DeadLinkChecker(
base_url=args.url,
max_pages=args.max_pages,
timeout=args.timeout,
delay=args.delay,
)
success = checker.run()
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()