23 releases

0.1.22 Jul 8, 2021
0.1.21 Dec 29, 2020
0.1.14 Jul 12, 2020
0.1.13 Jun 26, 2020
0.1.9 Feb 27, 2020

#168 in Web programming

Download history 4/week @ 2021-06-29 20/week @ 2021-07-06 7/week @ 2021-07-13 53/week @ 2021-07-20 53/week @ 2021-07-27 32/week @ 2021-08-03 41/week @ 2021-08-10 1/week @ 2021-08-17 10/week @ 2021-08-24 2/week @ 2021-08-31 5/week @ 2021-09-07 4/week @ 2021-09-14 4/week @ 2021-09-21 24/week @ 2021-09-28 4/week @ 2021-10-05 29/week @ 2021-10-12

59 downloads per month

MIT license

25KB
517 lines

Crabler - Web crawler for Crabs

CI Crates.io docs.rs MIT licensed

Asynchronous web scraper engine written in rust.

Features:

  • fully based on async-std
  • derive macro based api
  • struct based api
  • stateful scraper (structs can hold state)
  • ability to download files
  • ability to schedule navigation jobs in an async manner

Example

extern crate crabler;

use crabler::*;

#[derive(WebScraper)]
#[on_response(response_handler)]
#[on_html("a[href]", walk_handler)]
struct Scraper {}

impl Scraper {
    async fn response_handler(&self, response: Response) -> Result<()> {
        if response.url.ends_with(".jpg") && response.status == 200 {
            println!("Finished downloading {} -> {}", response.url, response.download_destination);
        }
        Ok(())
    }

    async fn walk_handler(&self, response: Response, a: Element) -> Result<()> {
        if let Some(href) = a.attr("href") {
            // attempt to download an image
            if href.ends_with(".jpg") {
                let p = Path::new("/tmp").join("image.jpg");
                let destination = p.to_string_lossy().to_string();

                if !p.exists() {
                    println!("Downloading {}", destination);
                    // schedule crawler to download file to some destination
                    // downloading will happen in the background, await here is just to wait for job queue
                    response.download_file(href, destination).await?;
                } else {
                    println!("Skipping exist file {}", destination);
                }
            } else {
              // or schedule crawler to navigate to a given url
              response.navigate(href).await?;
            };
        }

        Ok(())
    }
}

#[async_std::main]
async fn main() -> Result<()> {
    let scraper = Scraper {};

    // Run scraper starting from given url and using 20 worker threads
    scraper.run(Opts::new().with_urls(vec!["https://www.rust-lang.org/"]).with_threads(20)).await
}

Sample project

Gonzih/apod-nasa-scraper-rs

Dependencies

~12MB
~238K SLoC