Created
January 24, 2020 15:42
-
-
Save rust-play/a3161985a57c0f389d0b7718bed9b532 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate futures; | |
extern crate reqwest; | |
extern crate scraper; | |
use futures::{future, Future}; | |
use scraper::{Html, Selector}; | |
use std::time::Instant; | |
async fn get_doc(url: &str) -> Result<String, Box<dyn std::error::Error>> { | |
let doc = reqwest::get(url).await?.text().await?; | |
Ok(doc) | |
} | |
fn parse_links(doc: &str) -> Vec<String> { | |
Html::parse_document(doc) | |
.select(&Selector::parse("a").unwrap()) | |
.map(|elm| { | |
if let Some(url) = elm.value().attr("href") { | |
String::from(url) | |
} else { | |
String::new() | |
} | |
}) | |
.filter(|s| s.starts_with("http")) | |
.collect() | |
} | |
async fn get_links(link: &str) -> Vec<String> { | |
match get_doc(link).await { | |
Ok(doc) => parse_links(&doc), | |
Err(_) => vec![], | |
} | |
} | |
#[tokio::main] | |
async fn main() { | |
let mut links: Vec<_> = vec![String::from("http://jbadavis.github.io")]; | |
let mut link_store = links.clone(); | |
println!("Starting at {:?}\n", links[0]); | |
for _i in 0..2 { | |
let futures: Vec<_> = links.iter().map(|link| get_links(&link)).collect(); | |
let f = future::join_all(futures); | |
f.map(|x| println!("{}", x)); | |
} | |
println!("{:?}", link_store.len()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment