■ 웹 사이트에서 이미지 파일을 다운로드하는 방법을 보여준다.
▶ Cargo.toml
1 2 3 4 5 6 7 8 9 10 11 12 |
[package] name = "test_project" version = "0.1.0" edition = "2021" [dependencies] tokio = { version = "1", features = ["full"] } reqwest = "0.11.4" scraper = "0.12" urlencoding = "2.1" |
▶ src/main.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 |
use std::fs; use std::io::Write; use scraper; use tokio::time; #[tokio::main] async fn main() { for title in ["test", "yiy"] { download_images_async(title).await; } } async fn download_images_async(title : &str) { let root_url : &str = "https://uta.pw/shodou"; let url : String = format! ( "{}/index.php?titles&show&title={}", root_url, urlencoding::encode(title) ); println!("GET : {}", url); let html_string : String = reqwest::get(url).await.unwrap().text().await.unwrap(); let html : scraper::Html = scraper::Html::parse_document(&html_string); let selector : scraper::Selector = scraper::Selector::parse(".articles img").unwrap(); for(index, element) in html.select(&selector).enumerate() { let source : &str = element.value().attr("src").unwrap(); let img_url : String = format!("{}/{}", root_url, source); println!("{}", img_url); let file_name : String = format!("{}_{}.png", title, index); let bytes = reqwest::get(img_url).await.unwrap().bytes().await.unwrap(); let mut file : fs::File = fs::File::create(file_name).unwrap(); file.write_all(&bytes).unwrap(); time::sleep(time::Duration::from_millis(1000)).await; } } |