-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathchrome_web_automation.rs
62 lines (51 loc) · 1.82 KB
/
chrome_web_automation.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! `cargo run --example chrome_web_automation --features chrome chrome_headed`
extern crate spider;
use std::time::Duration;
use spider::configuration::{WaitForIdleNetwork, WebAutomation};
use spider::features::chrome_common::RequestInterceptConfiguration;
use spider::hashbrown::HashMap;
use spider::tokio;
use spider::website::Website;
#[tokio::main]
async fn main() {
let mut automation_scripts = HashMap::new();
automation_scripts.insert(
"/en/blog".into(),
Vec::from([
WebAutomation::Evaluate(r#"document.body.style.background = "blue";"#.into()),
WebAutomation::ScrollY(2000),
WebAutomation::Click("article a".into()),
WebAutomation::Wait(5000),
WebAutomation::Screenshot {
output: "example.png".into(),
full_page: true,
omit_background: true,
},
]),
);
let mut website: Website = Website::new("https://rsseau.fr/en/blog")
.with_chrome_intercept(RequestInterceptConfiguration::new(true))
.with_wait_for_idle_network(Some(WaitForIdleNetwork::new(Some(Duration::from_secs(30)))))
.with_limit(1)
.with_automation_scripts(Some(automation_scripts))
.build()
.unwrap();
let mut rx2 = website.subscribe(16).unwrap();
tokio::spawn(async move {
while let Ok(page) = rx2.recv().await {
println!("{:?}", page.get_url());
}
});
let start = crate::tokio::time::Instant::now();
website.crawl().await;
let duration = start.elapsed();
let links = website.get_all_links_visited().await;
for link in links.iter() {
println!("- {:?}", link.as_ref());
}
println!(
"Time elapsed in website.crawl() is: {:?} for total pages: {:?}",
duration,
links.len()
)
}