我正在尝试遵循此处的示例: https://rust-lang-nursery.github.io/rust-cookbook/web/scraping.html,它同时利用 Reqwest 和 Select 来获取 html 响应然后解析数据.
我使用的是 Reqwest 版本 0.10.4 和 Select 版本 0.4.3,它们是示例中显示的版本。但是,我收到一个错误:
error[E0277]: the trait bound `reqwest::Response: std::io::Read` is not satisfied
--> src/main.rs:19:25
|
19 | Document::from_read(res)?
| ^^^ the trait `std::io::Read` is not implemented for `reqwest::Response`
|
::: /root/.cargo/registry/src/github.com-1ecc6299db9ec823/select-0.4.3/src/document.rs:31:25
|
31 | pub fn from_read<R: io::Read>(mut readable: R) -> io::Result<Document> {
| -------- required by this bound in `select::document::Document::from_read`
似乎 from_read 方法接受了 Read 类型,但 reqwest::get 方法返回了不同的类型。在将响应传递给 from_read 方法之前,是否必须先进行某种转换?
这是一个例子:
#[macro_use]
extern crate error_chain;
extern crate reqwest;
extern crate select;
use select::document::Document;
use select::predicate::Name;
error_chain! {
foreign_links {
ReqError(reqwest::Error);
IoError(std::io::Error);
}
}
fn main() -> Result<()> {
let res = reqwest::get("https://www.rust-lang.org/en-US/").await?;
Document::from_read(res)?
.find(Name("a"))
.filter_map(|n| n.attr("href"))
.for_each(|x| println!("{}", x));
Ok(())
}