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
|
self.stats.offsite_requests_count += 1
|
||||||
log.debug(f"Filtered offsite request to: {result.url}")
|
log.debug(f"Filtered offsite request to: {result.url}")
|
||||||
elif isinstance(result, dict):
|
elif isinstance(result, dict):
|
||||||
await self._handle_item(result)
|
self.stats.items_scraped += 1
|
||||||
|
self._items.append(result)
|
||||||
if self._item_stream:
|
if self._item_stream:
|
||||||
await self._item_stream.send(result)
|
await self._item_stream.send(result)
|
||||||
|
await self.spider.on_scraped_item(result)
|
||||||
log.debug(f"Scraped from {str(response)}\n{result}")
|
log.debug(f"Scraped from {str(response)}\n{result}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await self.spider.on_error(request, 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:
|
async def _task_wrapper(self, request: Request) -> None:
|
||||||
"""Wrapper to track active task count."""
|
"""Wrapper to track active task count."""
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -158,6 +158,10 @@ class Spider(ABC):
|
|||||||
"""
|
"""
|
||||||
self.logger.error(error, exc_info=error)
|
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:
|
async def is_blocked(self, response: "Response") -> bool:
|
||||||
"""Check if the response is blocked. Users should override this for custom detection logic."""
|
"""Check if the response is blocked. Users should override this for custom detection logic."""
|
||||||
if response.status in BLOCKED_CODES:
|
if response.status in BLOCKED_CODES:
|
||||||
|
|||||||
Reference in New Issue
Block a user