()
| 5 | |
| 6 | |
| 7 | async def main() -> None: |
| 8 | http_client = ImpitHttpClient( |
| 9 | # Optional additional keyword arguments for `impit.AsyncClient`. |
| 10 | http3=True, |
| 11 | browser='firefox', |
| 12 | verify=True, |
| 13 | ) |
| 14 | |
| 15 | crawler = ParselCrawler( |
| 16 | http_client=http_client, |
| 17 | # Limit the crawl to max requests. Remove or increase it for crawling all links. |
| 18 | max_requests_per_crawl=10, |
| 19 | ) |
| 20 | |
| 21 | # Define the default request handler, which will be called for every request. |
| 22 | @crawler.router.default_handler |
| 23 | async def request_handler(context: ParselCrawlingContext) -> None: |
| 24 | context.log.info(f'Processing {context.request.url} ...') |
| 25 | |
| 26 | # Enqueue all links from the page. |
| 27 | await context.enqueue_links() |
| 28 | |
| 29 | # Extract data from the page. |
| 30 | data = { |
| 31 | 'url': context.request.url, |
| 32 | 'title': context.selector.css('title::text').get(), |
| 33 | } |
| 34 | |
| 35 | # Push the extracted data to the default dataset. |
| 36 | await context.push_data(data) |
| 37 | |
| 38 | # Run the crawler with the initial list of URLs. |
| 39 | await crawler.run(['https://crawlee.dev']) |
| 40 | |
| 41 | |
| 42 | if __name__ == '__main__': |
no test coverage detected