|
14 | 14 |
|
15 | 15 | @docs_group('Classes')
|
16 | 16 | class Router(Generic[TCrawlingContext]):
|
17 |
| - """Dispatches requests to registered handlers based on their labels.""" |
| 17 | + """A request dispatching system that routes requests to registered handlers based on their labels. |
| 18 | +
|
| 19 | + The `Router` allows you to define and register request handlers for specific labels. When a request is received, |
| 20 | + the router invokes the corresponding `request_handler` based on the request's `label`. If no matching handler |
| 21 | + is found, the default handler is used. |
| 22 | +
|
| 23 | + ### Usage |
| 24 | +
|
| 25 | + ```python |
| 26 | + from crawlee.crawlers import HttpCrawler, HttpCrawlingContext |
| 27 | + from crawlee.router import Router |
| 28 | +
|
| 29 | + router = Router[HttpCrawlingContext]() |
| 30 | +
|
| 31 | +
|
| 32 | + # Handler for requests without a matching label handler |
| 33 | + @router.default_handler |
| 34 | + async def default_handler(context: HttpCrawlingContext) -> None: |
| 35 | + context.log.info(f'Request without label {context.request.url} ...') |
| 36 | +
|
| 37 | +
|
| 38 | + # Handler for category requests |
| 39 | + @router.handler(label='category') |
| 40 | + async def category_handler(context: HttpCrawlingContext) -> None: |
| 41 | + context.log.info(f'Category request {context.request.url} ...') |
| 42 | +
|
| 43 | +
|
| 44 | + # Handler for product requests |
| 45 | + @router.handler(label='product') |
| 46 | + async def product_handler(context: HttpCrawlingContext) -> None: |
| 47 | + context.log.info(f'Product {context.request.url} ...') |
| 48 | +
|
| 49 | +
|
| 50 | + async def main() -> None: |
| 51 | + crawler = HttpCrawler(request_handler=router) |
| 52 | + await crawler.run() |
| 53 | + """ |
18 | 54 |
|
19 | 55 | def __init__(self) -> None:
|
20 | 56 | self._default_handler: RequestHandler[TCrawlingContext] | None = None
|
|
0 commit comments