直接开干!
首先说明一下,我这只有Mac和Linux系统,Windows在最后做点补充说明。
一、故事的开始通常,打开终端,进入命令行,输入以下命令,回车执行:
curl -L https://t66y.com/htm_data/2603/7/7193659.html
大概率会显示如下信息:
![]()
但是,我用代理,浏览器是能正常打开的,能看到聚聚「涝汁海鲜」淫妻的大场面😄
![]()
那该怎么办呢?
二、有一种技术叫「Rust」「Rust 是一门系统级编程语言,目标是同时做到高性能 + 内存安全 + 并发安全。它由 Mozilla 最初发起,现在由 Rust Foundation 维护。」
突然来这么一句,那就意味着,我们要用Rust语言搞她。
三、安装环境继续在终端的命令行下执行下面命令:
curl https://sh.rustup.rs -sSf | sh
大概率会出现下面信息:
- curl https://sh.rustup.rs -sSf | sh
- info: downloading installer
- warn: It looks like you have an existing rustup settings file at:
- warn: /root/.rustup/settings.toml
- warn: Rustup will install the default toolchain as specified in the settings file,
- warn: instead of the one inferred from the default host triple.
- Welcome to Rust!
- //*
- 此处省略好多行
- *//
- 1) Proceed with standard installation (default - just press enter)
- 2) Customize installation
- 3) Cancel installation
- >
默认,直接回车安装。
等待安装执行完成后,会是这个结果:
- info: profile set to default
- info: default host triple is x86_64-unknown-linux-gnu
- info: syncing channel updates for stable-x86_64-unknown-linux-gnu
- info: latest update on 2026-03-26 for version 1.94.1 (e408947bf 2026-03-25)
- info: downloading 6 components
- cargo installed 10.46 MiB
- clippy installed 4.44 MiB
- rust-docs installed 20.60 MiB
- rust-std installed 28.46 MiB
- rustc installed 75.00 MiB
- rustfmt installed 2.11 MiB info: default toolchain set to stable-x86_64-unknown-linux-gnu
- stable-x86_64-unknown-linux-gnu installed - rustc 1.94.1 (e408947bf 2026-03-25)
- Rust is installed now. Great!
- ////后面就都省略了
恭喜,这就表示安装成功了。
然后继续执行下面命令:
source $HOME/.cargo/env
使当前安装立即生效
然后执行下面命令验证一下:
cargo --version
如果出现类似于:
cargo 1.94.1 (29ea6fb6a 2026-03-24)
表示Rust以及其执行命令cargo安装成功。
接下来,我们测试下运行程序:
- cargo new hello
- cd hello
- cargo run
如果您要是之前没搞过编程,大概率会出现:
- Creating binary (application) `hello` package
- note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
- Compiling hello v0.1.0 (/root/hello)
- error: linker `cc` not found
- |
- = note: No such file or directory (os error 2)
- error: could not compile `hello` (bin "hello") due to 1 previous erro
这说明,光成功安装了Rust和Cargo还不够,创建新项目并尝试运行cargo run时遇到了链接器错误linker 'cc' not found。这个问题与Cargo.toml的清单格式无关,而是由于您的系统缺少编译Rust项目所必需的C语言工具链(构建依赖)。
所以,还得需要安装build-essential软件包组,它包含了GCC、G++、make等基本的编译工具。
那么,还是在命令行,执行以下命令:
- sudo apt update
- sudo apt install build-essential
我就不贴执行情况了,如果中间提示:
Do you want to continue? [Y/n]
你就直接选择【Y】回车,继续安装。
直到结束所有进程,回到待机状态,就表示安装完成了。
在执行前面的命令:
cargo run
那么,八九不离十,就应该出现:
- Compiling hello v0.1.0 (/root/hello)
- Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.23s
- Running `target/debug/hello`
- Hello, world!
到此,所有条件已经具备。
三、这才是我们要干的:编程1、首先创建项目仍然在命令行执行下面命令:
- cd
- cargo new img_fetcher
- cd img_fetche
这三行表示,退回到你的初始目录,然后创建新目录,并进入该目录
新目录名,你可以随便改。
执行完后,会自动创建下面目录和文件
- ll
- total 24
- drwxr-xr-x 4 root root 4096 Mar 27 01:15 ./
- drwx------ 10 root root 4096 Mar 27 01:15 ../
- -rw-r--r-- 1 root root 82 Mar 27 01:15 Cargo.toml
- drwxr-xr-x 6 root root 4096 Mar 27 01:15 .git/
- -rw-r--r-- 1 root root 8 Mar 27 01:15 .gitignore
- drwxr-xr-x 2 root root 4096 Mar 27 01:15 src
Cargo.toml是配置文件,src目录是我们要编写的程序,一个文件就行,他叫:「main.rs」
其它可以不用管了。
2、编辑并修改Cargo.toml我习惯用nano,命令行执行:
nano Cargo.toml
按住「ctrl+k」,删除所有行,然后复制粘贴下面内容到里面:
- [package]
- name = "img_fetcher"
- version = "0.1.0"
- edition = "2021"
- [dependencies]
- reqwest = { version = "0.11", features = ["blocking"] }
- scraper = "0.18"
然后保存退出,nano我习惯按「ctrl+x」,然后按提示,输入「y」回车,即可保存退出到命令行。
3、编辑主程序:main.rs
我仍然用nano编辑器,命令行执行:
nano ./src/main.rs
清空里面的内容,复制粘贴下面代码,保存退出:
- use reqwest::blocking::Client;
- use scraper::{Html, Selector};
- use std::fs::{create_dir_all, File};
- use std::io::{copy, stdin};
- fn main() -> Result<(), Box<dyn std::error::Error>> {
- // ===== 输入 =====
- let mut input = String::new();
- println!("输入URL:");
- stdin().read_line(&mut input)?;
- let url = input.trim().to_string();
- input.clear();
- println!("保存目录(默认 imgs):");
- stdin().read_line(&mut input)?;
- let dir = if input.trim().is_empty() {
- "imgs".to_string()
- } else {
- input.trim().to_string()
- };
- input.clear();
- println!("最小宽度px(默认 800):");
- stdin().read_line(&mut input)?;
- let min_width: u32 = if input.trim().is_empty() {
- 800
- } else {
- input.trim().parse().unwrap_or(800)
- };
- create_dir_all(&dir)?;
- // ===== 请求页面 =====
- let client = Client::builder().build()?;
- let resp = client
- .get(&url)
- .header("User-Agent", "Mozilla/5.0")
- .header("Cookie", "ismob=0")
- .send()?;
- let html = resp.text()?;
- // ===== 解析图片 =====
- let document = Html::parse_document(&html);
- let selector = Selector::parse("img").unwrap();
- let mut count = 0;
- for element in document.select(&selector) {
- if let Some(img_url) = element.value().attr("ess-data") {
- println!("下载: {}", img_url);
- let mut resp = client.get(img_url).send()?;
- let filename = format!("{}/{}.jpg", dir, count);
- let mut file = File::create(&filename)?;
- copy(&mut resp, &mut file)?;
- count += 1;
- }
- }
- println!("完成,共下载 {} 张图片", count);
- // ===== 说明 min_width =====
- println!("默认最小宽度为 {}px (当前版本不做实际过滤)", min_width);
- Ok(())
- }
四、执行程序在当前目录下,命令行执行:
cargo run
那么,系统会自动编译程序:
![]()
不出意外的话,最终会出现:
![]()
然后我们按提示输入我们要下载页面的地址,比如:
https://t66y.com/htm_data/2603/7/7193659.html然后还可以按提示输入保存目录、图片最小宽度筛选,也可以直接回车,采用默认参数
我这就成功下载到了指定目录:
![]()
查看一下文件列表:
![]()
前面的数字表示大小,单位是K,紧挨着就是文件名,自动数字。
到此,主要功能就完成了。
五、Windows下咋办大概率应该是这样的:
在Windows上运行Rust程序,只需以下几个简单步骤:
1. 安装Rust工具链
访问
https://www.rust-lang.org/tools/install 下载并运行 rustup-init.exe,按提示完成安装。
2. 安装MSVC构建工具
安装 Visual Studio Installer,然后选择“使用C++的桌面开发”工作负载,确保勾选“Windows 10/11 SDK”和“MSVC v143 - VS 2022 C++ x64/x86 生成工具”。
3. 然后就可以参照前面的创建项目、编辑程序、执行
六、其它万一在安装、执行过程中遇到什么问题,建议把问题复制给AI,先让AI帮你解决,实在不行,再到此讨论。
内容冗长,耽搁大家时间,感谢捧场。
赞(91)