当前位置: 首页 > news >正文

使用 Rust 进行程序

首先,我们需要安装必要的库。在终端中运行以下命令来安装 `scraper` 和 `reqwest` 库:

```rust
cargo install scraper reqwest
```

然后,我们可以开始编写程序。以下是一个基本的爬虫程序,用于爬取 上的图片:

```rust
use reqwest;
use scraper::{html, parse};
use std::collections::HashSet;

fn main() {
    let proxy_host = 
    let proxy_port = 

    // 创建一个设置
    let proxy = reqwest::ProxyServer::new(proxy_host, proxy_port).unwrap();

    // 创建一个 HashSet 来存储我们已经访问过的 URL
    let mut visited_urls = HashSet::new();

    // 获取网页 HTML
    let mut response = proxy.get("https://www.10jqka.com.cn/").unwrap();
    response.set_proxy(proxy_host, proxy_port);

    let html = response.text().unwrap();

    // 解析 HTML 并提取图片 URL
    let urls = html
        .parse()
        .unwrap()
        .select(|node| node.is_text_node() && node.as_str().contains("src"))
        .map(|node| node.as_str().split(" ").nth(1).unwrap())
        .collect::<HashSet<String>>();

    // 访问每个 URL 并提取图片
    for url in urls {
        if !visited_urls.contains(url) {
            visited_urls.insert(url);

            // 下载图片
            let mut response = proxy.get(url).unwrap();
            response.set_proxy(proxy_host, proxy_port);

            let file = reqwest::cookies::CookieJar::new();
            let mut file = file.load("cookies.txt").unwrap();
            response.set_cookies(file);

            let body = response.text().unwrap();
            let mut file = File::create("image.jpg").unwrap();
            file.write_all(body.as_bytes()).unwrap();
        }
    }
}
```

这个程序首先创建了一个设置,然后使用 `reqwest` 库向目标网站发送请求并获取 HTML。

http://www.lryc.cn/news/221663.html

相关文章:

  • 第10章 增长和扩展你的 Micro SaaS 应用程序
  • 第八章《搞懂算法:逻辑回归是怎么回事》笔记
  • 【WinForm详细教程八】WinForm中的TreeView控件
  • 〔003〕虚幻 UE5 基础教程和蓝图入门
  • 如何像优秀测试人员那样思考?
  • NOIP2023模拟13联测34 A. origenNOIP2023模拟13联测34 A. origen
  • HttpClient学习(Java)
  • 信息系统项目管理师之各工具的定义及解释
  • golang的defer执行时机案例分析
  • 2.HTML中常用浏览器
  • Vue 监听store数据变化
  • 智能交通和自动驾驶技术
  • CentOS7安装部署StarRocks
  • 树形Dp 2925. 在树上执行操作以后得到的最大分数
  • 选择企业云盘?品牌推荐和评价解析
  • redis: 记录一次线上redis内存占用过大问题解决过程
  • 数据资产、数字资产、数据资源及数据资产入表
  • Docker之Centos安装
  • SQL注入漏洞:CMS布尔盲注python脚本编写
  • security
  • 了解web3,什么是web3
  • Harbor企业级Registry基础镜像仓库的详细安装使用教程(保姆级)
  • Linux系统下数据同步服务RSYNC
  • Docker介绍及其常用命令
  • SwissArmyTransformer瑞士军刀工具箱使用手册
  • unity【动画】脚本_角色动画控制器 c#
  • Java代码如何对Excel文件进行zip压缩
  • 改进YOLO系列:12.Repulsion损失函数【遮挡】
  • win11网络连接正常,但是无法正常上网
  • 硬科技企业社区“曲率引擎”品牌正式发布