본문 바로가기
Rust

[Rust] Scraping : Naver 메인 페이지 예제 (60일차)

by 꾸압 2023. 2. 20.

 

<예제 코드_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>

 

 

댓글