improve scraper runtime
This commit is contained in:
@@ -23,6 +23,7 @@ class NetworkCapture:
|
||||
_origin: str | None = None
|
||||
|
||||
def attach(self, page: Page) -> None:
|
||||
# Подписываемся на request/response события страницы.
|
||||
try:
|
||||
self._origin = urlparse(page.url).netloc.lower() or None
|
||||
except Exception:
|
||||
@@ -31,6 +32,7 @@ class NetworkCapture:
|
||||
page.on("response", self._on_response)
|
||||
|
||||
def _is_same_origin(self, url: str) -> bool:
|
||||
# При необходимости ограничиваемся same-origin и доменами IAAI.
|
||||
if not self.settings.gentle.capture_same_origin_only or not self._origin:
|
||||
return True
|
||||
netloc = urlparse(url).netloc.lower()
|
||||
@@ -45,6 +47,7 @@ class NetworkCapture:
|
||||
if len(self.requests) >= self.settings.gentle.max_requests:
|
||||
return
|
||||
key = f"{request.method}:{request.url}:{request.post_data or ''}"
|
||||
# Дедупликация одинаковых запросов.
|
||||
if key in self._seen_req:
|
||||
return
|
||||
self._seen_req.add(key)
|
||||
@@ -92,6 +95,7 @@ class NetworkCapture:
|
||||
|
||||
@staticmethod
|
||||
def _categorize(url: str) -> str:
|
||||
# Простая эвристика для разбивки ответов по смыслу.
|
||||
low = url.lower()
|
||||
mapping = {
|
||||
"images": ["image", "media", "photos", "gallery"],
|
||||
|
||||
Reference in New Issue
Block a user