Skip to content

Commit 0cb1ff3

Browse files
committed
update docstring for Router
1 parent 37f7610 commit 0cb1ff3

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/crawlee/router.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,40 @@
1414

1515
@docs_group('Classes')
1616
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+
"""
1851

1952
def __init__(self) -> None:
2053
self._default_handler: RequestHandler[TCrawlingContext] | None = None

uv.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)