从网站下载并获取HTML代码

时间:2015-10-30 19:39:05

标签: parsing web rust

我需要从某个网页下载HTML代码。处理此任务的最佳方法是什么?据我所知,现在很少有适用于Rust的工作网框架,超级用户是大多数人使用的?但在搜索了它的文档后,我找不到办法。我得到的最接近的是

extern crate hyper;

use hyper::Client;

fn main() {
    let client = Client::new();
    let res = client.get("http://www.bloomberg.com/")
        .send()
        .unwrap();

    println!("{:?}", res);
}

但它返回Response,它似乎不包含HTML正文中的任何代码。

1 个答案:

答案 0 :(得分:4)

Note: this answer is outdated!


It's a bit hidden: The Response type implements the trait Read. One method of Read is read_to_string which reads everything into the a String. That's a simple way you can get the body.

extern crate hyper;

use hyper::Client;
use std::io::Read;

fn main() {
    let client = Client::new();
    let mut res = client.get("http://www.bloomberg.com/")
        .send()
        .unwrap();
    let mut body = String::new();
    res.read_to_string(&mut body).expect("failed to read into string");
    println!("{}", body);
}

Currently Rustdoc (the HTML documentation of Rust) is a little bit misleading because Rust beginners think that trait implementations don't add any important functionality. This is not true, so better look out for it. However, the hyper documentation could be better...