Hello, first time post, I am scrapping my university site for course schedule but can't get pass the validation code at login, I am using reqwest and scrapper(this is also using tauri)
code:
lazy_static! {
static ref CLIENT: Mutex<Client> = Mutex::new(Client::builder()
.cookie_store(true)
.build()
.expect("Failed to create client"));
}
#[tauri::command] //gets the capcha image to display in front end
fn get_captcha() -> Result<String, String> {
let client = CLIENT.lock().unwrap();
let login_page = client
.get("https://uni link here/")
.send()
.and_then(|res| res.text())
.map_err(|e| e.to_string())?;
let fragment = Html::parse_document(&login_page);
let captcha_selector = Selector::parse("img#imgVC").unwrap();
let captcha_url = fragment
.select(&captcha_selector)
.next()
.and_then(|e| e.value().attr("src"))
.unwrap_or("");
if captcha_url.is_empty() {
return Err("CAPTCHA image not found".to_string());
}
let full_captcha_url = format!("university link here{}", captcha_url);
Ok(full_captcha_url)
}
#[tauri::command]//executed after user input account info and capcha
fn login(user: String, pass: String, cap: String) -> Result<String, String> {
let client = CLIENT.lock().unwrap();
let mut
form_data = HashMap::new();
form_data.insert("stuid", user);
form_data.insert("SPassword", pass);
// this part is the issue, i tried converting it to i32 instead of string but it is still wrong
form_data.insert("ValidCode", cap_int.to_string());
let response = client
.post("university_login check_link.asp")
.form(&form_data)
.send()
.map_err(|e| e.to_string())?;
let cookies = response
.headers()
.get_all(reqwest::header::SET_COOKIE)
.iter()
.map(|val| val.to_str().unwrap_or(""))
.collect::<Vec<_>>();
println!("Cookies received: {:?}", cookies); //check cookies
let response_html = response.text().map_err(|e| e.to_string())?; // get entire page html
Ok(response_html)
}
this code did sent the login info, but the validation code is always wrong, as indicated by the html page returned by the login function and the empty cookie, I have no idea whether it is because of a wrong type, or the session changed and the code got refreshed, any help would be appreciated, thank you.
also I did try to create it with thiryfour, but want a solution without webdrivers
Edit: I found the main issue, It is that the validation code changes with each request, and the reqwest that i have been sending have been changing the validation code