没有jsoup,rust怎么解析html呢?

在 Rust 中,你可以使用各种库来解析网页内容。一个常用的库是 reqwest ,它提供了一个简单的方式来发送 HTTP 请求并获取网页内容。另外,你可以使用 scraperselect 等库来解析 HTML 或 XML 格式的网页内容。

下面是一个使用 reqwestscraper 库解析网页内容的示例:

首先,将以下内容添加到你的 Cargo.toml 文件中:

bash 复制代码
[dependencies]
reqwest = "0.11"
scraper = "0.12"

然后,创建一个 Rust 文件,并添加以下代码:

bash 复制代码
use reqwest::blocking::get;
use scraper::{Html, Selector};
 fn main() {
    // 发送 HTTP GET 请求获取网页内容
    let response = get("https://example.com").expect("Failed to send request");
    let body = response.text().expect("Failed to get response body");
     // 使用 scraper 解析 HTML
    let document = Html::parse_document(&body);
    let selector = Selector::parse("h1").expect("Failed to parse selector");
     // 提取特定元素的内容
    let h1_text = document.select(&selector).next().map(|element| element.text().collect::<String>());
     // 打印提取的内容
    if let Some(text) = h1_text {
        println!("H1 Text: {}", text);
    } else {
        println!("No H1 element found");
    }
}

在这个示例中,我们使用 reqwest 库发送 HTTP GET 请求并获取网页内容。然后,我们使用 scraper 库解析 HTML 内容。在这个示例中,我们使用 Selector 来选择 <h1> 元素,并提取其文本内容。

下面我们再看下 Selector 的其他用法,下面是三个使用 scraper 库的 Selector 类的示例,分别用于解析出 <p> 标签、解析出指定 class 的元素以及解析出指定 id 的元素。

  1. 解析出 <p> 标签:
rust 复制代码
use scraper::{Html, Selector};
 fn main() {
    let html = r#"
        <html>
            <body>
                <div>
                    <p>Paragraph 1</p>
                    <p>Paragraph 2</p>
                </div>
            </body>
        </html>
    "#;
     let document = Html::parse_document(html);
    let selector = Selector::parse("p").unwrap();
     for element in document.select(&selector) {
        let text = element.text().collect::<String>();
        println!("Text: {}", text);
    }
}
  1. 解析出指定 class 的元素:
rust 复制代码
use scraper::{Html, Selector};
 fn main() {
    let html = r#"
        <html>
            <body>
                <div>
                    <p class="highlight">Paragraph 1</p>
                    <p>Paragraph 2</p>
                </div>
            </body>
        </html>
    "#;
     let document = Html::parse_document(html);
    let selector = Selector::parse("p.highlight").unwrap();
     for element in document.select(&selector) {
        let text = element.text().collect::<String>();
        println!("Text: {}", text);
    }
}
  1. 解析出指定 id 的元素:
rust 复制代码
use scraper::{Html, Selector};
 fn main() {
    let html = r#"
        <html>
            <body>
                <div>
                    <p id="my-paragraph">Paragraph 1</p>
                    <p>Paragraph 2</p>
                </div>
            </body>
        </html>
    "#;
     let document = Html::parse_document(html);
    let selector = Selector::parse("#my-paragraph").unwrap();
     for element in document.select(&selector) {
        let text = element.text().collect::<String>();
        println!("Text: {}", text);
    }
}
相关推荐
咸甜适中9 小时前
rust的docx-rs库,自定义docx模版批量生成docx文档(逐行注释)
开发语言·rust·docx·docx-rs
FAFU_kyp12 小时前
RISC0_ZERO项目在macOs上生成链上证明避坑
开发语言·后端·学习·macos·rust
古城小栈15 小时前
开发常用 宏
算法·rust
咸甜适中15 小时前
rust的docx-rs库读取docx文件中的文本内容(逐行注释)
开发语言·rust·docx·docx-rs
Fleshy数模16 小时前
零基础玩转HTML:核心标签与页面构建
python·html
siwangdexie_new16 小时前
html格式字符串转word文档,前端插件( html-docx-js )遇到兼容问题的解决过程
前端·javascript·html
Surplusx18 小时前
运用VS Code前端开发工具完成微博发布案例
前端·html
外派叙利亚18 小时前
uniapp canvas 自定义仪表盘 可滑动 可点击 中间区域支持自定义
前端·javascript·uni-app·html
无法长大18 小时前
Mac M1 环境下使用 Rust Tauri 将 Vue3 项目打包成 APK 完整指南
android·前端·macos·rust·vue3·tauri·打包apk
新缸中之脑18 小时前
Google:Rust实战评估
开发语言·后端·rust