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

How to modify this program, i want get the next page link

本文关键字:想得到 链接 一页 程序 何修改 修改      更新时间:2023-09-26
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
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);
}

为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答案是正确的