如何修改这个程序,我希望得到下一页的链接

时间:2015-07-29 05:35:14

标签: javascript node.js asynchronous

var request = require('request');
var cheerio = require('cheerio');
var path = require('path');

var page = 1;
var proxiedRequest = request.defaults({ proxy: "http://jpyoip01.mgmt.ericsson.se:8080" });

down();

function down(){
    var url = 'http://tommyton.tumblr.com';
    (function woker(url){
        console.log(url);
        proxiedRequest.get(url, function(err, response, body){
            $ = cheerio.load(body);
            $('.photo-wrapper-inner img').each(function() {
                //console.log($(this).attr('src'));
            });

            var nextPage = $('#pagination a[class=next]').attr('href');
            //console.log(nextPage);

            woker(url + nextPage);
        });
    })(url);
}

输出:

    http://tommyton.tumblr.com
    http://tommyton.tumblr.com/page/2
    http://tommyton.tumblr.com/page/2/page/3
    http://tommyton.tumblr.com/page/2/page/3undefined
    http://tommyton.tumblr.com/page/2/page/3undefinedundefined
    http://tommyton.tumblr.com/page/2/page/3undefinedundefinedundefined

预期结果:

        http://tommyton.tumblr.com
        http://tommyton.tumblr.com/page/2
        http://tommyton.tumblr.com/page/3
        http://tommyton.tumblr.com/page/4
        http://tommyton.tumblr.com/page/5
        http://tommyton.tumblr.com/page/6

2 个答案:

答案 0 :(得分:1)

function down(){
    var url = 'http://tommyton.tumblr.com';
    (function woker(url2){
        console.log(url2);
        proxiedRequest.get(url2, function(err, response, body){
            $ = cheerio.load(body);
            $('.photo-wrapper-inner img').each(function() {
                //console.log($(this).attr('src'));
            });

            var nextPage = $('#pagination a[class=next]').attr('href');
            //console.log(nextPage);

            woker(url + nextPage);
        });
    })(url);
}

答案 1 :(得分:0)

重命名worker函数的url参数将解决问题。

var url = 'http://tommyton.tumblr.com';
    (function woker(myUrl){
        console.log(myUrl);
        proxiedRequest.get(myUrl, function(err, response, body){
            $ = cheerio.load(body);
            $('.photo-wrapper-inner img').each(function() {
                //console.log($(this).attr('src'));
            });

            var nextPage = $('#pagination a[class=next]').attr('href');
            //console.log(nextPage);

            woker(url + nextPage);
        });
    })(url);

HyoukJoon Lee 回答是正确的