feat: add hook to run after collecting an item
This commit is contained in:
@@ -102,18 +102,15 @@ class CrawlerEngine:
|
||||
self.stats.offsite_requests_count += 1
|
||||
log.debug(f"Filtered offsite request to: {result.url}")
|
||||
elif isinstance(result, dict):
|
||||
await self._handle_item(result)
|
||||
self.stats.items_scraped += 1
|
||||
self._items.append(result)
|
||||
if self._item_stream:
|
||||
await self._item_stream.send(result)
|
||||
await self.spider.on_scraped_item(result)
|
||||
log.debug(f"Scraped from {str(response)}\n{result}")
|
||||
except Exception as e:
|
||||
await self.spider.on_error(request, e)
|
||||
|
||||
async def _handle_item(self, item: dict[str, Any]) -> None:
|
||||
"""Handle a scraped item. Override or extend for item pipelines."""
|
||||
self.stats.items_scraped += 1
|
||||
self._items.append(item)
|
||||
|
||||
async def _task_wrapper(self, request: Request) -> None:
|
||||
"""Wrapper to track active task count."""
|
||||
try:
|
||||
|
||||
@@ -158,6 +158,10 @@ class Spider(ABC):
|
||||
"""
|
||||
self.logger.error(error, exc_info=error)
|
||||
|
||||
async def on_scraped_item(self, item: dict[str, Any]) -> None:
|
||||
"""Handle a scraped item. Override or extend for item pipelines."""
|
||||
pass
|
||||
|
||||
async def is_blocked(self, response: "Response") -> bool:
|
||||
"""Check if the response is blocked. Users should override this for custom detection logic."""
|
||||
if response.status in BLOCKED_CODES:
|
||||
|
||||
Reference in New Issue
Block a user