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