Skip to content

feat: adding filters that filter based on regular expressions of crawl4ai.deep_crawling.filters #999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions crawl4ai/deep_crawling/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ def _update_stats(self, passed: bool):
self.stats._counters[1] += passed # passed
self.stats._counters[2] += not passed # rejected

class RegexURLFilter(URLFilter):
"""
A filter that uses regular expressions to determine whether a URL matches a given request.
The URLFilter inherited from Crawl4AI.
"""

def __init__(self, patterns: List[str], name: str = None):
super().__init__(name)
self.patterns = [re.compile(p) for p in patterns]

def apply(self, url: str) -> bool:
for pattern in self.patterns:
if pattern.search(url):
self._update_stats(True)
return True
self._update_stats(False)
return False

class FilterChain:
"""Optimized filter chain"""
Expand Down