This commit is contained in:
lychang
2024-01-12 03:23:51 +08:00
commit daa46336f1
2 changed files with 40 additions and 0 deletions

22
src/web_parser.rs Normal file
View File

@@ -0,0 +1,22 @@
use scraper::{Html, Selector};
use crate::{file, get_response};
pub(crate) fn get_html_title(body: String) -> String {
let document = Html::parse_document(&body);
let title_selector = Selector::parse("h1").unwrap();
let title_element = document.select(&title_selector).next().unwrap();
return title_element.text().next().unwrap().to_string();
}
pub(crate) fn download_image(url_path: &str, file_path: &str) -> bool {
let url = format!("https://i.tuiimg.net/{}", url_path);
println!("{}",url);
let response = get_response(&url);
if response.is_empty() {
return false
}
let file_path = format!("images/{}", file_path);
println!("{}",file_path);
file::write_file(&file_path, response).expect("Write file error");
return true
}