<예제 코드_1>
# Cargo.toml
# ... 중략
[dependencies]
reqwest = { version = "0.11.14", features = ["blocking"] }
scraper = "0.14.0"
// src\main.rs
use reqwest::blocking::Client;
use scraper::{Html, Selector};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create an HTTP client
let client = Client::new();
// Make a GET request to the homepage of www.naver.com
let res = client.get("https://www.naver.com").send()?;
// Extract the HTML response
let body = res.text()?;
// Parse the HTML response using the scraper crate
let document = Html::parse_document(&body);
// Use a CSS selector to find the elements that you want
let selector = Selector::parse(".nav_item").unwrap();
for element in document.select(&selector) {
// Extract the text content of each element
let text = element.text().collect::<String>();
println!("{}", text);
}
Ok(())
}
<참조 1> https://chat.openai.com/
<참조 2>
'Rust' 카테고리의 다른 글
[Rust] MySQL 연결 및 Data 적재 (62일차) (0) | 2023.03.26 |
---|---|
[Rust] fn main() -> Result<(), Box<dyn std::error::Error>> { } 가 무엇인가? (61일차) (0) | 2023.03.06 |
[Rust] Scraping 예제 (59일차) (0) | 2023.02.19 |
[Rust] Variable : Python 코드 비교 예제 (58일차) (0) | 2023.02.18 |
댓글