Repository navigation
Expand file tree
/
Copy pathcrawler.py
More file actions
54 lines (40 loc) · 1.81 KB
/
Copy pathcrawler.py
File metadata and controls
54 lines (40 loc) · 1.81 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
45
46
47
48
49
50
51
52
53
54
import asyncio
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import TypedDict
from fastapi import FastAPI
from crawlee.crawlers import ParselCrawler, ParselCrawlingContext
class State(TypedDict):
"""State available in the app."""
crawler: ParselCrawler
requests_to_results: dict[str, asyncio.Future[dict[str, str]]]
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[State]:
# Start up code that runs once when the app starts
# Results will be stored in this dictionary
requests_to_results = dict[str, asyncio.Future[dict[str, str]]]()
crawler = ParselCrawler(
# Keep the crawler alive even when there are no more requests to process now.
# This makes the crawler wait for more requests to be added later.
keep_alive=True
)
# Define the default request handler, which will be called for every request.
@crawler.router.default_handler
async def request_handler(context: ParselCrawlingContext) -> None:
context.log.info(f'Processing {context.request.url} ...')
title = context.selector.xpath('//title/text()').get() or ''
# Extract data from the page and save it to the result dictionary.
requests_to_results[context.request.unique_key].set_result(
{
'title': title,
}
)
# Start the crawler without awaiting it to finish
crawler.log.info(f'Starting crawler for the {app.title}')
run_task = asyncio.create_task(crawler.run([]))
# Make the crawler and the result dictionary available in the app state
yield {'crawler': crawler, 'requests_to_results': requests_to_results}
# Cleanup code that runs once when the app shuts down
crawler.stop()
# Wait for the crawler to finish
await run_task