forked from jianshuo/my_own_crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebCrawler.py
More file actions
345 lines (276 loc) · 11.8 KB
/
WebCrawler.py
File metadata and controls
345 lines (276 loc) · 11.8 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlparse, urlunparse
import time
import os
import hashlib
from collections import deque
import concurrent.futures
import threading
class WebCrawler:
def __init__(
self,
seed_url,
max_depth=5,
restrict_to_domain=True,
save_path=None,
max_workers=10,
rate_limit=0.1,
):
self.visited_urls = set()
self.link_structure = {}
self.max_depth = max_depth
self.restrict_to_domain = restrict_to_domain
parsed_url = urlparse(seed_url)
self.base_domain = parsed_url.netloc
self.save_path = save_path
self.url_to_filename_map = {} # Maps URLs to their filenames
self.max_workers = max_workers # Number of worker threads
self.rate_limit = rate_limit # Time delay between requests to same domain
# Locks for thread safety
self.visited_lock = threading.Lock()
self.structure_lock = threading.Lock()
self.file_lock = threading.Lock()
# Domain access timestamps to implement rate limiting
self.domain_timestamps = {}
self.domain_lock = threading.Lock()
# Create save directory if specified
if self.save_path and not os.path.exists(self.save_path):
os.makedirs(self.save_path)
def normalize_url(self, url):
"""Normalize a URL by removing fragments and standardizing format."""
try:
# Parse the URL
parsed = urlparse(url)
# Remove the fragment
normalized = urlunparse(
(
parsed.scheme,
parsed.netloc,
parsed.path,
parsed.params,
parsed.query, # Query parameters are preserved
"", # Empty fragment
)
)
# Ensure path ends with / if it's empty
if parsed.path == "":
normalized = normalized + "/"
return normalized
except Exception:
return url
def crawl(self):
"""Crawl the website starting from seed_url and return the link structure."""
seed_url = (
f"https://{self.base_domain}"
if not urlparse(self.base_domain).scheme
else self.base_domain
)
# Normalize the seed URL
seed_url = self.normalize_url(seed_url)
# Initialize the work queue with the seed URL
queue = deque([(seed_url, 1)])
# Track URLs currently being processed to avoid duplicates
processing = set()
processing_lock = threading.Lock()
# Process URLs in batches until no more work
with concurrent.futures.ThreadPoolExecutor(
max_workers=self.max_workers
) as executor:
while queue:
# Get a batch of URLs to process
batch = []
with processing_lock:
while queue and len(batch) < self.max_workers:
url, depth = queue.popleft()
# Skip if already visited or being processed
if url in self.visited_urls or url in processing:
continue
# Skip if depth exceeds max_depth
if depth > self.max_depth:
continue
# Mark as being processed
processing.add(url)
batch.append((url, depth))
if not batch:
break
# Submit batch to thread pool
future_to_url = {
executor.submit(self.process_url, url, depth): (url, depth)
for url, depth in batch
}
# Process completed futures and collect new URLs
new_urls = []
for future in concurrent.futures.as_completed(future_to_url):
url, depth = future_to_url[future]
with processing_lock:
processing.remove(url)
try:
# Get the results (new URLs to crawl)
result_urls = future.result()
if result_urls:
# Add new URLs to process
for new_url in result_urls:
new_urls.append((new_url, depth + 1))
except Exception as e:
print(f"Error processing {url}: {e}")
# Add new URLs to the queue
with processing_lock:
queue.extend(new_urls)
return self.link_structure
def process_url(self, url, depth):
"""Process a single URL and return new URLs to crawl."""
# Normalize the URL (remove fragments)
url = self.normalize_url(url)
# Check if URL is valid
try:
parsed = urlparse(url)
if not parsed.scheme or not parsed.netloc:
return []
# Check if already visited (double-check with lock)
with self.visited_lock:
if url in self.visited_urls:
return []
self.visited_urls.add(url)
# Initialize entry in link structure
with self.structure_lock:
self.link_structure[url] = []
print(f"Crawling: {url} (depth: {depth})")
# Apply rate limiting for the domain
domain = parsed.netloc
self.apply_rate_limit(domain)
# Check for cached content
cached_content = None
if self.save_path:
filename = self.get_filename_for_url(url)
filepath = os.path.join(self.save_path, filename)
# Use file lock when checking/reading cache
with self.file_lock:
if os.path.exists(filepath):
print(f"Using cached version for: {url}")
try:
with open(filepath, "r", encoding="utf-8") as f:
cached_content = f.read()
except Exception as e:
print(f"Error reading cache for {url}: {e}")
cached_content = None
# Fetch or use cached content
if cached_content:
links = self.extract_links_from_content(url, cached_content)
else:
links, content = self.fetch_links_and_content(url)
# Save content if needed
if self.save_path and content:
self.save_page(url, content)
# Update link structure
with self.structure_lock:
self.link_structure[url] = links
# Return new URLs to crawl (make sure they're normalized too)
new_urls = []
for link in links:
# Normalize the link
link = self.normalize_url(link)
with self.visited_lock:
if link in self.visited_urls:
continue
link_domain = urlparse(link).netloc
if not self.restrict_to_domain or link_domain == self.base_domain:
new_urls.append(link)
return new_urls
except Exception as e:
print(f"Error processing {url}: {e}")
return []
def apply_rate_limit(self, domain):
"""Apply rate limiting for requests to the same domain."""
with self.domain_lock:
current_time = time.time()
if domain in self.domain_timestamps:
# Calculate how long to wait
last_access = self.domain_timestamps[domain]
elapsed = current_time - last_access
if elapsed < self.rate_limit:
time.sleep(self.rate_limit - elapsed)
# Update timestamp
self.domain_timestamps[domain] = time.time()
def extract_links_from_content(self, url, content):
"""Extract links from HTML content."""
soup = BeautifulSoup(content, "html.parser")
links = []
for a_tag in soup.find_all("a", href=True):
href = a_tag["href"]
# Skip empty hrefs, javascript links, and anchors
if not href or href.startswith("javascript:") or href.startswith("#"):
continue
try:
# Try to join to create an absolute URL
absolute_url = urljoin(url, href)
# Normalize the URL (remove fragments)
absolute_url = self.normalize_url(absolute_url)
# Validate URL has a scheme (http/https)
parsed = urlparse(absolute_url)
if not parsed.scheme or not parsed.netloc:
continue
links.append(absolute_url)
except Exception:
# If URL parsing fails, skip this URL
continue
return links
def fetch_links_and_content(self, url):
"""Fetch links and content from a URL."""
headers = {"User-Agent": "PythonWebCrawler/1.0"}
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
# Store the content
content = response.text
# Parse links
links = self.extract_links_from_content(url, content)
return links, content
def get_filename_for_url(self, url):
"""Generate a consistent filename for a URL."""
# Normalize URL first to remove fragments
url = self.normalize_url(url)
if url in self.url_to_filename_map:
return self.url_to_filename_map[url]
# Create a unique filename from the URL
parsed_url = urlparse(url)
# Remove scheme and www if present
domain = parsed_url.netloc.replace("www.", "")
# Hash the path for unique filenames
path_hash = hashlib.md5(parsed_url.path.encode()).hexdigest()[:8]
# Create query hash if query exists
query_part = ""
if parsed_url.query:
query_part = f"_q_{parsed_url.query.replace('?','_').replace('=','_').replace('&','_')}"
# Create a sensible filename
if parsed_url.path and parsed_url.path != "/":
path = parsed_url.path.rstrip("/")
filename = f"{domain}{path.replace('/', '_')}{query_part}"
if len(filename) > 100: # Limit filename length
filename = f"{domain}_{path_hash}{query_part}"
else:
filename = f"{domain}_index{query_part}"
# Ensure filename is valid and add extension
filename = "".join(c if c.isalnum() or c in "_-." else "_" for c in filename)
filename = f"{filename}.html"
# Store in map for consistency
with self.file_lock:
self.url_to_filename_map[url] = filename
return filename
def save_page(self, url, content):
"""Save the page content to a file."""
filename = self.get_filename_for_url(url)
file_path = os.path.join(self.save_path, filename)
with self.file_lock:
with open(file_path, "w", encoding="utf-8") as f:
f.write(content)
def print_link_structure(self):
"""Print the discovered link structure."""
print("\nWebsite Link Structure:")
print("=======================")
for url, links in sorted(self.link_structure.items(), key=lambda x: x[0]):
print(f"\n📄 {url}")
print(f" Outgoing links ({len(links)}):")
for link in sorted(links):
print(f" → {link}")
print("\nSummary:")
print(f"Total pages crawled: {len(self.visited_urls)}")