File tree Expand file tree Collapse file tree 1 file changed +34
-1
lines changed Expand file tree Collapse file tree 1 file changed +34
-1
lines changed Original file line number Diff line number Diff line change 14
14
15
15
@docs_group ('Classes' )
16
16
class Router (Generic [TCrawlingContext ]):
17
- """Dispatches requests to registered handlers based on their labels."""
17
+ """Dispatches requests to registered handlers based on their labels.
18
+
19
+ Create a `Router` instance and decorate handlers with it, specifying the `label` parameter to correctly process
20
+ requests requiring different logic. Pass it to the crawler as the `request_handler` parameter.
21
+
22
+ ```python
23
+ from crawlee.crawlers import HttpCrawler, HttpCrawlingContext
24
+ from crawlee.router import Router
25
+
26
+ router = Router[HttpCrawlingContext]()
27
+
28
+
29
+ # Handler for requests without a matching label handler
30
+ @router.default_handler
31
+ async def basic_handler(context: HttpCrawlingContext) -> None:
32
+ context.log.info(f'Request without label {context.request.url} ...')
33
+
34
+
35
+ # Handler for category requests
36
+ @router.handler(label='category')
37
+ async def a_handler(context: HttpCrawlingContext) -> None:
38
+ context.log.info(f'Category request {context.request.url} ...')
39
+
40
+
41
+ # Handler for product requests
42
+ @router.handler(label='product')
43
+ async def b_handler(context: HttpCrawlingContext) -> None:
44
+ context.log.info(f'Product {context.request.url} ...')
45
+
46
+
47
+ crawler = HttpCrawler(request_handler=router)
48
+
49
+ await crawler.run()
50
+ """
18
51
19
52
def __init__ (self ) -> None :
20
53
self ._default_handler : RequestHandler [TCrawlingContext ] | None = None
You can’t perform that action at this time.
0 commit comments