-
Notifications
You must be signed in to change notification settings - Fork 714
Expand file tree
/
Copy pathrl_tandem_example_explicit.py
More file actions
44 lines (31 loc) · 1.35 KB
/
rl_tandem_example_explicit.py
File metadata and controls
44 lines (31 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import asyncio
from crawlee.crawlers import ParselCrawler, ParselCrawlingContext
from crawlee.request_loaders import RequestList, RequestManagerTandem
from crawlee.storages import RequestQueue
async def main() -> None:
# Create a static request list.
request_list = RequestList(['https://crawlee.dev', 'https://apify.com'])
# Open the default request queue.
request_queue = await RequestQueue.open()
# And combine them together to a single request manager.
request_manager = RequestManagerTandem(request_list, request_queue)
# Create a crawler and pass the request manager to it.
crawler = ParselCrawler(
request_manager=request_manager,
max_requests_per_crawl=10, # Limit the max requests per crawl.
)
@crawler.router.default_handler
async def handler(context: ParselCrawlingContext) -> None:
context.log.info(f'Processing {context.request.url}')
# New links will be enqueued directly to the queue.
await context.enqueue_links()
# Extract data using Parsel's XPath and CSS selectors.
data = {
'url': context.request.url,
'title': context.selector.xpath('//title/text()').get(),
}
# Push extracted data to the dataset.
await context.push_data(data)
await crawler.run()
if __name__ == '__main__':
asyncio.run(main())