-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_crawler.py
More file actions
447 lines (370 loc) · 17.9 KB
/
web_crawler.py
File metadata and controls
447 lines (370 loc) · 17.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlparse
import os
import re
import time
import argparse
from collections import deque
class WebsiteCrawler:
def __init__(self, start_url, output_file, delay=1.0, max_pages=None, max_depth=None, path_prefix=None, respect_robots=True):
"""
Initialize the crawler with the starting URL and configuration.
Args:
start_url (str): The URL to start crawling from
output_file (str): Path to save the extracted content
delay (float): Delay between requests in seconds
max_pages (int, optional): Maximum number of pages to crawl
max_depth (int, optional): Maximum depth to crawl (levels from start URL)
path_prefix (str, optional): Only crawl URLs with this path prefix
respect_robots (bool): Whether to respect robots.txt
"""
self.start_url = start_url
self.base_domain = urlparse(start_url).netloc
self.output_file = output_file
self.delay = delay
self.max_pages = max_pages
self.max_depth = max_depth
self.path_prefix = path_prefix
self.respect_robots = respect_robots
# Sets to keep track of URLs
self.visited_urls = set()
self.urls_to_visit = deque([(start_url, 0)]) # (url, depth) pairs
# Dictionary to store extracted content
self.extracted_content = {}
# Disallowed paths (from robots.txt)
self.disallowed_paths = []
if respect_robots:
self._parse_robots_txt()
def _parse_robots_txt(self):
"""Parse robots.txt to respect website crawling rules"""
parsed_url = urlparse(self.start_url)
robots_url = f"{parsed_url.scheme}://{parsed_url.netloc}/robots.txt"
try:
response = requests.get(robots_url)
if response.status_code == 200:
lines = response.text.split('\n')
# Simple robots.txt parsing
for line in lines:
if line.lower().startswith('disallow:'):
path = line.split(':', 1)[1].strip()
if path:
self.disallowed_paths.append(path)
print(f"Found {len(self.disallowed_paths)} disallowed paths in robots.txt")
else:
print("No robots.txt found or couldn't be accessed")
except Exception as e:
print(f"Error parsing robots.txt: {e}")
def _is_allowed(self, url):
"""Check if URL is allowed to be crawled based on robots.txt rules"""
if not self.respect_robots:
return True
parsed_url = urlparse(url)
path = parsed_url.path
for disallowed in self.disallowed_paths:
if path.startswith(disallowed):
return False
return True
def _is_valid_url(self, url):
"""Check if URL should be crawled (same domain, not visited, allowed by robots.txt)"""
parsed_url = urlparse(url)
# Check if URL is from the same domain
if parsed_url.netloc != self.base_domain:
return False
# Check if URL has already been visited
if url in self.visited_urls:
return False
# Check if URL path starts with the required prefix
if self.path_prefix and not parsed_url.path.startswith(self.path_prefix):
return False
# Check file extensions to avoid PDFs, images, etc.
if parsed_url.path.lower().endswith(('.pdf', '.doc', '.docx', '.jpg', '.jpeg', '.png', '.gif', '.css', '.js')):
return False
# Check if URL is allowed by robots.txt
if not self._is_allowed(url):
return False
return True
def _process_text_node(self, element):
"""Process a text node, removing extra whitespace"""
if element.string:
return element.string.strip()
return ""
def _process_element(self, element, indent=0):
"""Process an HTML element and its children recursively to extract structured content"""
if element.name is None:
# This is a text node
text = self._process_text_node(element)
if text:
return text
return ""
# Skip script, style, and other non-content elements
if element.name in ['script', 'style', 'meta', 'link', 'noscript']:
return ""
# Skip site navigation and header elements
skip_classes = ['nav__global', 'nav__page', 'site-search-wrap', 'flyout-menu', 'header-primary', 'leftSidebar']
for cls in skip_classes:
if element.get('class') and cls in element.get('class'):
return ""
# Handle various HTML elements differently
if element.name == 'h1':
return f"\n# {element.get_text(strip=True)}\n\n"
elif element.name == 'h2':
return f"\n## {element.get_text(strip=True)}\n\n"
elif element.name == 'h3':
return f"\n### {element.get_text(strip=True)}\n\n"
elif element.name == 'h4':
return f"\n#### {element.get_text(strip=True)}\n\n"
elif element.name == 'h5':
return f"\n##### {element.get_text(strip=True)}\n\n"
elif element.name == 'h6':
return f"\n###### {element.get_text(strip=True)}\n\n"
elif element.name == 'p':
return f"{element.get_text(separator=' ', strip=True)}\n\n"
elif element.name == 'br':
return "\n"
elif element.name == 'hr':
return "\n---\n\n"
elif element.name == 'a' and element.has_attr('href'):
text = element.get_text(strip=True)
href = element['href']
if text:
return f"[{text}]({href})"
return ""
elif element.name in ['ul', 'ol']:
result = "\n"
for i, li in enumerate(element.find_all('li', recursive=False)):
li_text = ""
for child in li.children:
li_text += self._process_element(child, indent)
if element.name == 'ol':
result += f"{i+1}. {li_text.strip()}\n"
else:
result += f"* {li_text.strip()}\n"
return result + "\n"
elif element.name == 'details':
# Special handling for accordion elements
summary = element.find('summary')
summary_text = summary.get_text(strip=True) if summary else "Accordion Item"
# Process all content inside the details tag, excluding the summary
content = ""
for child in element.children:
if child != summary:
content += self._process_element(child, indent)
return f"\n### {summary_text}\n{content}\n"
elif element.name == 'summary':
# Skip summary elements as they're handled by the details processing
return ""
elif element.name == 'table':
# Enhanced table handling
result = "\n"
# First, handle table headers
headers = []
header_row = element.find('thead')
if header_row:
th_cells = header_row.find_all('th')
if th_cells:
headers = [th.get_text(strip=True) for th in th_cells]
if not headers:
# Try to get headers from the first row
first_row = element.find('tr')
if first_row:
th_cells = first_row.find_all('th')
if th_cells:
headers = [th.get_text(strip=True) for th in th_cells]
# If we found headers, add them to the table
if headers:
result += "| " + " | ".join(headers) + " |\n"
result += "| " + " | ".join(['---'] * len(headers)) + " |\n"
# Process rows
rows = element.find_all('tr')
for row in rows:
# Skip the header row if we already processed it
if row == element.find('tr') and headers and row.find('th'):
continue
cells = row.find_all(['td', 'th'])
if cells:
result += "| " + " | ".join(cell.get_text(strip=True) for cell in cells) + " |\n"
return result + "\n"
elif element.name == 'div':
# Process div content
result = ""
for child in element.children:
result += self._process_element(child, indent)
return result
# For other elements, process their children
result = ""
for child in element.children:
result += self._process_element(child, indent + 1)
return result
def _extract_content(self, url, soup):
"""Extract relevant content from the page using a structured approach"""
# Title of the page
title = soup.title.string if soup.title else "No Title"
# Get main content
main_content = None
# Try to find the main content area with various selectors, prioritizing more specific IDs/classes
content_selectors = [
'#main', # Main content div on ETSU pages
'section[role="main"]', # Main section with role=main
'#content', # Content ID
'.content-grid', # Content grid class
'main', # HTML5 main tag
'article', # Article tag
'.page-content', # Page content class
'.main-content' # Main content class
]
for selector in content_selectors:
elements = soup.select(selector)
if elements:
main_content = elements[0]
break
# If no main content was found, use the body
if not main_content:
main_content = soup.body
# Remove navigation and sidebar elements that might be inside the main content
for nav in main_content.find_all(['nav', 'header', 'footer']):
nav.decompose()
# Check if there's an accordion list specifically (common on ETSU research pages)
accordion_lists = main_content.select('.list__accordions')
# Special handling if we found accordion lists
if accordion_lists:
structured_content = "# " + title + "\n\n"
for accordion_list in accordion_lists:
for details in accordion_list.find_all('details'):
summary = details.find('summary')
if summary:
summary_text = summary.get_text(strip=True)
structured_content += f"## {summary_text}\n\n"
# Extract content from the div inside details
content_div = details.find('div')
if content_div:
# Process paragraphs
for p in content_div.find_all('p'):
structured_content += f"{p.get_text(strip=True)}\n\n"
# Process lists
for ul in content_div.find_all('ul'):
for li in ul.find_all('li'):
# Handle links in list items
if li.find('a'):
for a in li.find_all('a'):
href = a.get('href', '')
link_text = a.get_text(strip=True)
structured_content += f"* [{link_text}]({href})\n"
else:
structured_content += f"* {li.get_text(strip=True)}\n"
structured_content += "\n"
else:
# Process the main content to extract structured text
structured_content = self._process_element(main_content)
# Clean up any excessive whitespace
structured_content = re.sub(r'\n{3,}', '\n\n', structured_content)
return {
'title': title,
'url': url,
'content': structured_content
}
def _extract_links(self, soup, current_url):
"""Extract links from the page"""
links = []
for anchor in soup.find_all('a', href=True):
href = anchor['href']
# Convert relative URLs to absolute
absolute_url = urljoin(current_url, href)
# Remove fragments and query parameters
parsed_url = urlparse(absolute_url)
clean_url = f"{parsed_url.scheme}://{parsed_url.netloc}{parsed_url.path}"
if self._is_valid_url(clean_url) and clean_url not in self.urls_to_visit:
links.append(clean_url)
return links
def crawl(self):
"""Start crawling the website"""
page_count = 0
print(f"Starting crawl from {self.start_url}")
print(f"Output will be saved to {self.output_file}")
if self.max_depth is not None:
print(f"Maximum crawl depth: {self.max_depth} levels from start URL")
if self.path_prefix:
print(f"Only crawling URLs with path prefix: {self.path_prefix}")
while self.urls_to_visit and (self.max_pages is None or page_count < self.max_pages):
# Get the next URL and its depth to visit
current_url, current_depth = self.urls_to_visit.popleft()
# Skip if already visited
if current_url in self.visited_urls:
continue
# Skip if we've reached max depth
if self.max_depth is not None and current_depth > self.max_depth:
continue
try:
# Make the request
depth_indicator = '*' * current_depth
print(f"Crawling [{current_depth}]: {depth_indicator} {current_url}")
response = requests.get(current_url)
# Mark as visited
self.visited_urls.add(current_url)
page_count += 1
# Process only successful responses
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Extract content
content = self._extract_content(current_url, soup)
self.extracted_content[current_url] = content
# Extract links if we haven't reached max depth
if self.max_depth is None or current_depth < self.max_depth:
links = self._extract_links(soup, current_url)
for link in links:
# Store URLs with their depth
url_entry = (link, current_depth + 1)
url_exists = any(link == u for u, _ in self.urls_to_visit)
if link not in self.visited_urls and not url_exists:
self.urls_to_visit.append(url_entry)
else:
print(f"Failed to fetch {current_url}: HTTP {response.status_code}")
# Respect the delay between requests
time.sleep(self.delay)
except Exception as e:
print(f"Error crawling {current_url}: {e}")
# Save the extracted content
self._save_content()
print(f"Crawling complete. Visited {page_count} pages.")
print(f"Content saved to {self.output_file}")
def _save_content(self):
"""Save the extracted content to a Markdown file"""
with open(self.output_file, 'w', encoding='utf-8') as f:
f.write(f"# ETSU Computing Department Website Content\n\n")
f.write(f"*Crawled on {time.strftime('%Y-%m-%d %H:%M:%S')}*\n\n")
f.write(f"*Starting URL: {self.start_url}*\n\n")
# Write content for each URL
for url, data in self.extracted_content.items():
f.write(f"## {data['title']}\n\n")
f.write(f"**URL:** {url}\n\n")
f.write(f"{data['content']}\n\n")
f.write("---\n\n")
def main():
parser = argparse.ArgumentParser(description='Crawl a website and extract content to Markdown')
parser.add_argument('--start-url', type=str, default='https://www.etsu.edu/cbat/computing/',
help='URL to start crawling from')
parser.add_argument('--output', type=str, default='etsu_computing_content.md',
help='Output Markdown file')
parser.add_argument('--delay', type=float, default=1.0,
help='Delay between requests in seconds')
parser.add_argument('--max-pages', type=int, default=None,
help='Maximum number of pages to crawl')
parser.add_argument('--max-depth', type=int, default=None,
help='Maximum depth to crawl (1 = only links on start page)')
parser.add_argument('--path-prefix', type=str, default='/cbat/computing',
help='Only crawl URLs with this path prefix')
parser.add_argument('--no-robots', action='store_true',
help='Ignore robots.txt rules')
args = parser.parse_args()
crawler = WebsiteCrawler(
start_url=args.start_url,
output_file=args.output,
delay=args.delay,
max_pages=args.max_pages,
max_depth=args.max_depth,
path_prefix=args.path_prefix,
respect_robots=not args.no_robots
)
crawler.crawl()
if __name__ == "__main__":
main()