feat(spiders): Change items hook to make it for processing items + add a stat for this
This commit is contained in:
@@ -102,12 +102,17 @@ 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):
|
||||||
self.stats.items_scraped += 1
|
processed_result = await self.spider.on_scraped_item(result)
|
||||||
self._items.append(result)
|
if processed_result:
|
||||||
if self._item_stream:
|
self.stats.items_scraped += 1
|
||||||
await self._item_stream.send(result)
|
log.debug(f"Scraped from {str(response)}\n{processed_result}")
|
||||||
await self.spider.on_scraped_item(result)
|
if self._item_stream:
|
||||||
log.debug(f"Scraped from {str(response)}\n{result}")
|
await self._item_stream.send(processed_result)
|
||||||
|
else:
|
||||||
|
self._items.append(processed_result)
|
||||||
|
else:
|
||||||
|
self.stats.items_dropped += 1
|
||||||
|
log.warning(f"Dropped from {str(response)}\n{processed_result}")
|
||||||
elif result is not None:
|
elif result is not None:
|
||||||
log.error(f"Spider must return Request, dict or None, got '{type(result)}' in {request}")
|
log.error(f"Spider must return Request, dict or None, got '{type(result)}' in {request}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ class CrawlStats:
|
|||||||
offsite_requests_count: int = 0
|
offsite_requests_count: int = 0
|
||||||
response_bytes: int = 0
|
response_bytes: int = 0
|
||||||
items_scraped: int = 0
|
items_scraped: int = 0
|
||||||
|
items_dropped: int = 0
|
||||||
start_time: float = 0.0
|
start_time: float = 0.0
|
||||||
end_time: float = 0.0
|
end_time: float = 0.0
|
||||||
download_delay: float = 0.0
|
download_delay: float = 0.0
|
||||||
@@ -85,6 +86,7 @@ class CrawlStats:
|
|||||||
def to_dict(self) -> dict[str, Any]:
|
def to_dict(self) -> dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"items_scraped": self.items_scraped,
|
"items_scraped": self.items_scraped,
|
||||||
|
"items_dropped": self.items_dropped,
|
||||||
"elapsed_seconds": round(self.elapsed_seconds, 2),
|
"elapsed_seconds": round(self.elapsed_seconds, 2),
|
||||||
"download_delay": round(self.download_delay, 2),
|
"download_delay": round(self.download_delay, 2),
|
||||||
"concurrent_requests": self.concurrent_requests,
|
"concurrent_requests": self.concurrent_requests,
|
||||||
|
|||||||
@@ -160,9 +160,9 @@ 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:
|
async def on_scraped_item(self, item: Dict[str, Any]) -> Dict[str, Any] | None:
|
||||||
"""Handle a scraped item. Override or extend for item pipelines."""
|
"""A hook to be overridden by users to do some processing on scraped items, return `None` to drop the item silently."""
|
||||||
pass
|
return item
|
||||||
|
|
||||||
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."""
|
||||||
|
|||||||
Reference in New Issue
Block a user