-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsite.py
More file actions
95 lines (76 loc) · 2.78 KB
/
Copy pathsite.py
File metadata and controls
95 lines (76 loc) · 2.78 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
import requests
from bs4 import BeautifulSoup # type: ignore
from urllib.parse import urljoin, urlparse
import time
# Configuration
BASE_URL = "https://scet.ac.in"
DOMAIN = urlparse(BASE_URL).netloc
HEADERS = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
# Statistics
visited = set()
to_visit = [BASE_URL]
failed = set()
def is_valid(url):
"""Check if the URL is within the target domain and is likely an HTML page."""
parsed = urlparse(url)
# Stay within domain
if parsed.netloc != DOMAIN and not parsed.netloc.endswith(f".{DOMAIN}"):
return False
# Exclude common non-html file extensions
excluded_extensions = [
'.pdf', '.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg',
'.zip', '.tar', '.gz', '.7z', '.rar',
'.docx', '.xlsx', '.pptx', '.doc', '.xls', '.ppt',
'.mp3', '.mp4', '.wav', '.avi', '.mov',
'.css', '.js', '.xml', '.json'
]
path = parsed.path.lower()
if any(path.endswith(ext) for ext in excluded_extensions):
return False
return True
def clean_url(url):
"""Remove fragments and trailing slashes for normalization."""
parsed = urlparse(url)
# Remove fragment
cleaned = parsed._replace(fragment="").geturl()
# Remove trailing slash
if cleaned.endswith("/") and len(urlparse(cleaned).path) > 1:
cleaned = cleaned.rstrip("/")
return cleaned
print(f"Starting crawl of {BASE_URL}...")
while to_visit:
url = to_visit.pop(0)
url = clean_url(url)
if url in visited or url in failed:
continue
print(f"Crawling: {url} (Found: {len(visited)}, Remaining: {len(to_visit)})")
try:
r = requests.get(url, headers=HEADERS, timeout=10)
visited.add(url)
# Only parse HTML content
content_type = r.headers.get('Content-Type', '').lower()
if 'text/html' not in content_type:
continue
soup = BeautifulSoup(r.text, "html.parser")
for link in soup.find_all("a", href=True):
full_url = urljoin(url, link["href"])
full_url = clean_url(full_url)
if is_valid(full_url) and full_url not in visited and full_url not in to_visit:
to_visit.append(full_url)
# Be nice to the server
time.sleep(0.1)
except Exception as e:
print(f"Failed to crawl {url}: {e}")
failed.add(url)
# Save results to file
output_file = "sitemap.txt"
with open(output_file, "w") as f:
for url in sorted(list(visited)):
f.write(f"{url}\n")
print("-" * 30)
print(f"Crawl complete!")
print(f"Total pages found: {len(visited)}")
print(f"Failed URLs: {len(failed)}")
print(f"Results saved to: {output_file}")