[dependencies]
regex = "1.5.4"
reqwest = { version = "0.11", features = ["blocking", "json"] }
use regex::Regex;
use reqwest::blocking::get;
use std::fs::File;
use std::io::copy;
use std::io::Error;
fn main() -> Result<(), Error> {
// 示例文本
let text = r#"
https://xxxxx/1.jpg
https://xxxxx/a4b.png
https://xxxxx/4b.jpeg
"#;
// 正则表达式匹配图片链接
let re = Regex::new(r"https://\S+\.(jpg|png|jpeg)").unwrap();
// 遍历匹配到的链接
for cap in re.captures_iter(text) {
let url = &cap[0];
println!("匹配到的图片链接: {}", url);
// 下载图片
let response = get(url).unwrap();
let mut file = File::create(url.split('/').last().unwrap()).unwrap();
copy(&mut response.bytes().unwrap().as_ref(), &mut file).unwrap();
println!("图片已下载: {}", url.split('/').last().unwrap());
}
Ok(())
}
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
txttool.com 说一段 esp56物联 查询128 IP查询