Skip to content
Snippets Groups Projects
Commit 3cf0e9df authored by Marvin Weiler's avatar Marvin Weiler
Browse files

Geckodriver integration and scrapping amd shop site

Connecting to the amd-website via geckodriver and scrpaing the shop-articles.
Also filtering works rudimentarilly.
parents
Branches main
No related tags found
No related merge requests found
[package]
name = "amd-scrapper"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
fantoccini = "0.18.0"
scraper = "0.12.0"
tokio = { version = "1", features = ["full"] }
lettre = "0.9"
lettre_email = "0.9"
File added
use fantoccini::error::CmdError;
use fantoccini::{ClientBuilder, Locator};
#[tokio::main]
async fn main() {
let filters = vec!["RX 6800", "RX 6700"];
loop {
let products = get_html_products().await.unwrap();
let filtered: Vec<Product> = products
.into_iter()
.filter(|el| {
for s in &filters {
if el.name.contains(s) {
return el.available;
}
}
false
})
.collect();
if filtered.len() > 0 {
println!("The following products are available");
} else {
println!("Sorry, all products are out of stock. :/");
}
}
}
#[derive(Debug)]
pub struct Product {
name: String,
price: String,
available: bool,
}
async fn get_html_products() -> Result<Vec<Product>, CmdError> {
// navigate to amd-site
let mut webc = ClientBuilder::native()
.connect("http://localhost:4444")
.await
.expect("failed to connect to WebDriver");
webc.goto("https://www.amd.com/de/direct-buy/de")
.await
.expect("Could not connect to the amd Website");
// find all articles that are for sale
let html_elements = webc
.find_all(Locator::Css("article"))
.await
.expect("find_all error");
//Interate over all items in the shop and parse the relevant strings
// in the struct Product
let mut products: Vec<Product> = Vec::new();
for mut el in html_elements {
let name = el.find(Locator::Css(".shop-title")).await?.text().await?;
let price = el.find(Locator::Css(".shop-price")).await?.text().await?;
let avail = el.find(Locator::Css(".shop-links")).await?.text().await?;
let product = Product {
name,
price,
available: !avail.contains("ergriffen"),
};
products.push(product);
}
// we do not need the webbrowser anymore
webc.close()
.await
.expect("Failed to terminate webdriver sessions");
return Ok(products);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment