0

有没有人使用 Node Cheerio 来抓取整个网站,而不仅仅是抓取器指向的主页/首页?

目前我正在做以下仅抓取目标页面的操作。

request('http://arandomsite.com/', function (error, response, html) {
    if (!error && response.statusCode == 200){
        var $ = cheerio.load(html);
            ...
            ...
            ...
};
4

1 回答 1

1

我从未使用过 Cheerio,但我会假设(与其他刮刀一样),它只会执行您指向的页面。假设 Cheerio.load 返回类似 api 的 jquery,您可能需要执行类似的操作

$('a').each(function(index, a) {
    //TODO: You may want to keep track here of which you have done, and not redo any.
    request('http://arandomsite.com' + a.attr('href'), myPageProcessFunction);
});

显然,您还需要添加 iframe 之类的内容,以确保获得完整的结果。

为了澄清,这里是一些更新的代码:

request('http://arandomsite.com/', function responseFunction(error, response, html) {
if (!error && response.statusCode == 200){
    var $ = cheerio.load(html);
    $('a').each(function(index, a) {
        request('http://arandomsite.com' + a.attr('href'), responseFunction);
    });
};
});
于 2013-11-26T16:30:36.567 回答