silkworm.types¶
Public type aliases and protocols for annotating silkworm code.
Import from here rather than from private modules:
from silkworm.types import Callback, JSONValue, Logger
async def process_item(self, item: JSONValue, spider: Spider) -> JSONValue: ...
- class silkworm.types.BatchItemPipeline[source]
Bases:
ItemPipeline,ProtocolItem pipeline that can process an explicit batch in one call.
- type silkworm.types.BodyData = bytes | bytearray | memoryview | str | Mapping[str, JSONValue] | Iterable[tuple[str, str]] | list[JSONValue] | None
- class silkworm.types.CrawlResult[source]
Bases:
objectOutcome of a finished crawl, returned by
Engine.run()and runners.- spider
Spider name.
- Type:
- close_reason
Why the crawl ended:
"finished"when the queue drained,"shutdown"after a stop request or signal, a limit name such as"max_items", or the reason passed toCloseSpider.- Type:
- elapsed_seconds
Wall-clock crawl duration.
- Type:
- labeled_stats
Per-label breakdowns (see
BASE_LABELED_COUNTERS).
- custom_stats
A copy of the spider’s
stats_payload.- Type:
Mapping[str, JSONValue]
- property ok: bool
Return whether the crawl met its failure policy.
- property requests_sent: int
Return the number of requests handed to the HTTP client.
- property responses_received: int
Return the number of responses received.
- property items_scraped: int
Return the number of items that passed every pipeline.
- property items_dropped: int
Return the number of items discarded by pipelines or limits.
- property errors: int
Return the number of unrecovered request or callback failures.
- property error_rate: float
Return
errors / requests_sent(0.0when nothing was sent).
- property item_drop_rate: float
Return
items_dropped / (items_scraped + items_dropped).
- __init__(spider, close_reason, elapsed_seconds, stats, labeled_stats, custom_stats=<factory>, failures=())
- class silkworm.types.EngineOptions[source]
Bases:
TypedDictKeyword options for
Engine, also accepted by every runner.run_spider(MySpider, concurrency=32, request_timeout=10)forwards these toEngine; omitted keys use theEnginedefaults. Supplyhttp_clientto inject a compatible client; its concurrency then controls worker count and default queue capacity.
- class silkworm.types.ExceptionMiddleware[source]
Bases:
ProtocolProtocol for middleware that may recover from request failures.
- async process_exception(request, exception, spider)[source]
Return a retry request, or
Noneto leave the error unhandled.
- __init__(*args, **kwargs)
- class silkworm.types.FetchClient[source]
Bases:
ProtocolInterface the engine needs from an HTTP client.
HttpClient,CDPClient,ServoFetchClient,OnionLinkClient, andCachingHttpClientimplement it; pass any conforming object ashttp_client.- property concurrency: int
Return the maximum number of requests in flight.
- property html_max_size_bytes: int
Return the HTML document parsing limit in bytes.
- async fetch(req)[source]
Send
reqand return its response.
- async close()[source]
Release transport resources.
- Return type:
None
- __init__(*args, **kwargs)
- type silkworm.types.ItemCallback = Callable[[JSONValue, Spider], JSONValue | Awaitable[JSONValue | None] | None]
- class silkworm.types.ItemPipeline[source]
Bases:
ProtocolProtocol implemented by ordered item-processing stages.
- async open(spider)[source]
Allocate resources once before the first item is processed.
- Parameters:
spider (Spider)
- Return type:
None
- async close(spider)[source]
Flush buffered data and release resources after the crawl.
- Parameters:
spider (Spider)
- Return type:
None
- async process_item(item, spider)[source]
Process and return the item passed to the next pipeline.
- Parameters:
item (JSONValue)
spider (Spider)
- Return type:
JSONValue
- __init__(*args, **kwargs)
- type silkworm.types.ItemSchema = ModelSchema | type[object] | ItemValidator
- type silkworm.types.ItemValidator = Callable[[JSONValue], JSONValue]
- class silkworm.types.Logger[source]
Bases:
ProtocolStructured logger returned by
get_logger()andSpider.log.Keyword arguments become structured context fields on each record.
- configure(*, handlers=None)[source]
Replace the logger’s handlers with the supplied configurations.
Each handler mapping accepts
sink,level,serialize, andcolorize. A sink may be"stderr","stdout", a filesystem path, or a writable text stream.
- bind(**context)[source]
Return a logger carrying
contexton every subsequent record.
- info(message, **context)[source]
Emit an informational record with structured context.
- debug(message, **context)[source]
Emit a diagnostic record with structured context.
- warning(message, **context)[source]
Emit a warning record with structured context.
- error(message, **context)[source]
Emit an error record with structured context.
- exception(message, **context)[source]
Emit an error record including the active exception traceback.
- complete()[source]
Flush all configured handlers.
- Return type:
None
- __init__(*args, **kwargs)
- type silkworm.types.LoopFactory = Callable[[], AbstractEventLoop]
- class silkworm.types.ModelSchema[source]
Bases:
ProtocolA Pydantic-style model class:
model_validatereturns a model instance.- model_validate(obj)[source]
Validate
objand return a model instance, raising on failure.
- __init__(*args, **kwargs)
- type silkworm.types.QueryValue = str | int | float | bool | None | Iterable[str | int | float | bool | None]
- class silkworm.types.RequestMiddleware[source]
Bases:
ProtocolProtocol for middleware applied before an HTTP request is sent.
- async process_request(request, spider)[source]
Return the request to send, optionally modified or replaced.
- __init__(*args, **kwargs)