Your first crawl | rust-crawler | Spatie       

 SPATIE  

rust-crawler
============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Rust-crawler](https://spatie.be/docs/rust-crawler/v1)  Basic-usage  Your first crawl

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/rust-crawler/v1) 

  Your first crawl    
- [ Introduction ](https://spatie.be/docs/rust-crawler/v1/introduction)
- [ Installation &amp; setup ](https://spatie.be/docs/rust-crawler/v1/installation-setup)
- [ Support us ](https://spatie.be/docs/rust-crawler/v1/support-us)
- [ Questions and issues ](https://spatie.be/docs/rust-crawler/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/rust-crawler/v1/changelog)
- [ About us ](https://spatie.be/docs/rust-crawler/v1/about-us)

Basic usage
-----------

- [ Your first crawl ](https://spatie.be/docs/rust-crawler/v1/basic-usage/starting-your-first-crawl)
- [ Crawl responses ](https://spatie.be/docs/rust-crawler/v1/basic-usage/handling-crawl-responses)
- [ Using observers ](https://spatie.be/docs/rust-crawler/v1/basic-usage/using-observers)
- [ Collecting URLs ](https://spatie.be/docs/rust-crawler/v1/basic-usage/collecting-urls)
- [ Filtering URLs ](https://spatie.be/docs/rust-crawler/v1/basic-usage/filtering-urls)
- [ Testing ](https://spatie.be/docs/rust-crawler/v1/basic-usage/testing)
- [ Tracking progress ](https://spatie.be/docs/rust-crawler/v1/basic-usage/tracking-progress)

Configuring the crawler
-----------------------

- [ Concurrency &amp; throttling ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/crawl-behavior)
- [ Limits ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/setting-crawl-limits)
- [ Extracting resources ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/extracting-resources)
- [ Configuring requests ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/configuring-requests)
- [ Response filtering ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/handling-responses)
- [ Respecting robots.txt ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/respecting-robots-txt)

Advanced usage
--------------

- [ JavaScript rendering ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/rendering-javascript)
- [ Custom link extraction ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/extracting-custom-links)
- [ Custom request handlers ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/custom-request-handlers)
- [ Crawling across requests ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/crawling-across-requests)
- [ Custom crawl queue ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/using-a-custom-crawl-queue)
- [ Graceful shutdown ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/graceful-shutdown)

 Your first crawl
================

###  On this page 

1. [ Events ](#content-events)
2. [ Defaults ](#content-defaults)

Build a crawler, start it with `crawl()`, and keep reading events until the stream ends:

```
use spatie_crawler::{CrawlEvent, Crawler};

#[tokio::main]
async fn main() -> Result {
    let mut crawl = Crawler::builder("https://example.com")?
        .max_pages(100)
        .max_depth(3)
        .build()?
        .crawl();

    while let Some(event) = crawl.next().await {
        match event {
            CrawlEvent::Page { page, .. } => println!("{}: {}", page.url, page.status),
            CrawlEvent::Failed { url, error, .. } => eprintln!("{}: {error}", url.url),
            CrawlEvent::Finished { reason, error, .. } => {
                println!("Finished: {reason:?}");
                if let Some(error) = error {
                    return Err(error.into());
                }
            }
            _ => {}
        }
    }
    Ok(())
}
```

`Crawler::builder()` validates the seed URL. Builder methods consume and return the builder; `build()` validates the remaining configuration. `crawl()` consumes the crawler and starts a background scheduler on the current Tokio runtime.

`Crawl` has an async `next()` method and implements `Stream`. You do not need a stream extension import for the example above. Request failures arrive as events rather than an error from `next()`. Also inspect the final event's `error`, which can report scheduler or storage failures.

Events
--------------------------------------------------------------------------

EventMeaning`Started`The store has initialized and scheduling is starting.`Page`A fetched, extracted page is available.`Failed`A URL job failed after applicable retries; HTTP failures retain a response.`Skipped`A URL was refused or its page output was suppressed; inspect `reason`.`Discovered`A mapping crawl found a target URL without downloading it.`MalformedUrl`A page contained an invalid URL reference.`Finished`Scheduling has stopped; inspect the finish reason and optional error.Every event contains a progress snapshot. See [tracking progress](tracking-progress).

Defaults
--------------------------------------------------------------------------------

The crawler stays on the seed's exact hostname, runs up to 10 URL jobs concurrently and respects robots rules. Page and depth limits are unlimited unless you set them. The example deliberately sets both.

The seed has depth zero. Results arrive in concurrent completion order, not URL order. Network checks block private destinations by default.

The event buffer defaults to eight. Slow consumers apply backpressure; collecting all events into a vector gives up that memory advantage. Do not simply drop the stream when you need a completed checkpoint or final statistics. See [graceful shutdown](../advanced-usage/graceful-shutdown).

For short synchronous callbacks, use [observers](using-observers). For async storage or processing, consume the stream or implement a [custom request handler](../advanced-usage/custom-request-handlers).

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
