先看一下main.rs源码
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = std::env::args();
let mut url = "".to_string();
for arg in args {
url = arg;
}
let output = "rust.md";
println!("抓取网页: {}", url);
let body = reqwest::blocking::get(url)?.text()?;
println!("从html转成markdown...");
let md = html2md::parse_html(&body);
fs::write(output, md.as_bytes()).unwrap();
println!("转完的文档已存进 {}.", output);
Ok(())
}上述代码改了点东西, 注意是从std::env::args()方法中拿到命令行输入, 所以如果不输入网址会通过Result<T, E>类型返回错误。
通过cargo生成Cargo.toml之后补充一下依赖表
[package]
name = "scrape_url"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version = "0.11", features = ["blocking"] }
html2md = "0.2"如果不能通过编译, 检查一下你的网络环境...