Web Scraping Library
This Rust library enables recursive web scraping, media downloading, and content extraction from websites. With robust error handling and media support, the library is designed for flexible and scalable use in various web scraping scenarios.
Features
- Recursive Scraping: Start scraping from any URL and recursively follow links.
- Media Downloading: Download images, videos, and other media assets.
- Content Extraction: Extract text, meta tags, forms, and JavaScript contents from web pages.
- Error Logging: Logs errors to a file for later analysis.
- Random Delays: Mimics human behavior by adding random delays between requests.
Installation
To install the library, add the following to your Cargo.toml:
[dependencies]
knee_scraper ="0.1.6"
reqwest = "0.12.7"
tokio = { version = "1.40.0", features = ["full", "fs"] }
Scrape based on 'keyword search' with => "knee_scraper::rec_scrape;" + new configuration options => "knee_scraper::ScraperConfig;"
use knee_scraper::{ run, ScraperConfig, rec_scrape };
use reqwest::{Client, header};
use std::collections::{HashSet, VecDeque};
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let client = Client::new();
let target_phrase = "Hardcore computer-science porn";
let url = "httpz://www.happythoughts.com/";
let mut visited = HashSet::new();
let config = Some(ScraperConfig::new(
true, 3, Some("Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X)...".to_string()), ));
config.set_follow_links(false);
config.set_max_depth(5);
config.set_user_agent(Some("UpdatedScraper/2.0(My new updated user agent, brain: CPU Unlimmited learning like a turing machine)...".to_string()));
println!("Updated follow links: {}", config.follow_links());
println!("Updated max depth: {}", config.max_depth());
println!("User agent: {:?}", config.user_agent());
rec_scrape(&url, &client, config.as_ref(), &mut visited, target_phrase).await;
println!("Scraping process completed for {}", url);
sleep(Duration::from_secs(2)).await;
}
Scrape with scrape_js_content() for APIkey or products and/or w/e
use knee_scraper::{rec_scrape, scrape_js_content, ScraperConfig};
use reqwest::{Client};
use std::collections::HashSet;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let client = Client::new();
let target_phrase = "Advanced AI algorithm";
let url = "https://www.futuristic-technology.com/";
let mut visited = HashSet::new();
let config = Some(ScraperConfig::new(
true, 3, Some("Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X)...".to_string()), ));
config.as_ref().map(|cfg| {
cfg.set_follow_links(false);
cfg.set_max_depth(5);
cfg.set_user_agent(Some(
"UpdatedScraper/2.0 (My new updated user agent, brain: CPU Unlimited learning like a Turing machine)...".to_string(),
));
});
rec_scrape(&url, &client, config.as_ref(), &mut visited, target_phrase).await;
let js_keywords = vec!["apiKey", "token", "secret"];
scrape_js_content(&url, &client, &js_keywords).await;
sleep(Duration::from_secs(2)).await;
}
run() Example - with vector of urls to start from
use knee_scraper::run;
use reqwest::Client;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let client = Client::new();
let urls = vec![
"https://example.com",
"https://exampl3e.com",
];
for &url in &urls {
println!("Starting the scraping process for {}", url);
run(url, &client).await;
println!("Scraping process completed for {}", url);
sleep(Duration::from_secs(2)).await;
}
}
Basic Recursive Scraping Examples
use knee_scraper::recursive_scrape;
use std::collections::HashSet;
use reqwest::Client;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let client = Client::new(); let mut visited = HashSet::new();
let base_url = "https://example.com";
recursive_scrape(base_url, &client, &mut visited).await;
recursive_scrape2(base_url, &client, &mut visited).await;
}
async fn recursive_scrape2(url: &str, client: &Client, visited: &mut HashSet<String>) {
if visited.contains(url) {
return; }
visited.insert(url.to_string());
let response = client.get(url).send().await.unwrap();
if response.status().is_success() {
let html = response.text().await.unwrap();
let links = knee_scraper::extract_links(&html, url);
println!("Scraped {} - Found {} links", url, links.len());
for link in links {
if !visited.contains(&link) {
recursive_scrape(&link, client, visited).await;
sleep(Duration::from_millis(500)).await; }
}
}
}
Example with Robots.txt, Open Directories, and Cookies
[dependencies]
knee_scraper = "0.1.6"
futures = "0.3.30"
rand = "0.8.5"
regex = "1.10.6"
reqwest = "0.12.7"
scraper = "0.20.0"
tokio = { version = "1.40.0", features = ["full", "fs"] }
url = "2.5.2"
use knee_scraper::{recursive_scrape, fetch_robots_txt, check_open_directories, fetch_with_cookies};
use reqwest::Client;
use std::collections::HashSet;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let url = "https://example.com";
let client = Client::new();
let mut visited = HashSet::new();
println!("Fetching robots.txt...");
fetch_robots_txt(url, &client).await;
println!("Checking open directories...");
check_open_directories(url, &client).await;
println!("Fetching page with cookies...");
fetch_with_cookies(url, &client).await;
println!("Starting recursive scrape...");
recursive_scrape(url, &client, &mut visited).await;
println!("Delaying to mimic human behavior...");
sleep(Duration::from_secs(3)).await;
println!("Scraping complete.");
}