[英]How to modify this program, i want get the next page link
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);
}
The output: 输出:
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
The expect result: 预期结果:
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);
}
renaming the url
parameter for worker function will solve the problem. 将url
参数重命名为worker函数将解决此问题。
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 answer is correct HyoukJoon Lee的回答是正确的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.