使用请求承诺移动到新页面的最佳方法?
Best method of moving to a new page with request-promise?
我正在修改请求承诺以抓取朋友的网页。我在他们的 GitHub 上使用 crawl a webpage better 示例。我到目前为止是这样的:
var rp = require('request-promise');
var cheerio = require('cheerio'); // Basically jQuery for node.js
var options = {
uri: 'https://friendspage.org',
transform: function(body) {
return cheerio.load(body);
}
};
rp(options)
.then(function($) {
// Process html like you would with jQuery...
var nxtPage = $("a[data-url$='nxtPageId']").attr('data');
// How do I use nxtPage here to go to that site
})
.catch(function(err) {
// Crawling failed or Cheerio choked...
});
去 nxtPage
的 link 的正确方法是什么?我仍然希望能够在其上使用 cheerio/jQuery。我是否需要在当前 then
函数中重复整个 var option = ...
事情?
包装在一个函数中并继续使用条件调用它,以便递归有时会中断。
(function repeatUntilAConditionIsMetInThen(uri = 'https://friendspage.org')
var options = {
uri,
transform: function(body) {
return cheerio.load(body);
}
};
rp(options)
.then(function($) {
var nxtPage = $("a[data-url$='nxtPageId']").attr('data');
//There should be some condition here otherwise it will be infinite loop
repeatUntilAConditionIsMetInThen(nxtPage);
})
.catch(function(err) {
});
})();
您可以创建自己的实用函数来创建您的选项,然后像这样调用 rp()
:
const rp = require('request-promise');
const cheerio = require('cheerio'); // Basically jQuery for node.js
// shared function
function getPage(url) {
const options = {
uri: url,
transform: function(body) {
return cheerio.load(body);
}
};
return rp(options);
}
getPage('https://friendspage.org').then($ => {
// Process html like you would with jQuery...
const nxtPage = $("a[data-url$='nxtPageId']").attr('data');
return getPage(nxtPage).then($ => {
// more processing here
});
}).catch(err => {
console.log(err);
// error handling here
});
这只是将您想在多个地方使用的代码分解为一个共享函数。与 rp()
或 cheerio
没有特别关系,只是 Javascript(或任何语言)中的常规代码分解。
我正在修改请求承诺以抓取朋友的网页。我在他们的 GitHub 上使用 crawl a webpage better 示例。我到目前为止是这样的:
var rp = require('request-promise');
var cheerio = require('cheerio'); // Basically jQuery for node.js
var options = {
uri: 'https://friendspage.org',
transform: function(body) {
return cheerio.load(body);
}
};
rp(options)
.then(function($) {
// Process html like you would with jQuery...
var nxtPage = $("a[data-url$='nxtPageId']").attr('data');
// How do I use nxtPage here to go to that site
})
.catch(function(err) {
// Crawling failed or Cheerio choked...
});
去 nxtPage
的 link 的正确方法是什么?我仍然希望能够在其上使用 cheerio/jQuery。我是否需要在当前 then
函数中重复整个 var option = ...
事情?
包装在一个函数中并继续使用条件调用它,以便递归有时会中断。
(function repeatUntilAConditionIsMetInThen(uri = 'https://friendspage.org')
var options = {
uri,
transform: function(body) {
return cheerio.load(body);
}
};
rp(options)
.then(function($) {
var nxtPage = $("a[data-url$='nxtPageId']").attr('data');
//There should be some condition here otherwise it will be infinite loop
repeatUntilAConditionIsMetInThen(nxtPage);
})
.catch(function(err) {
});
})();
您可以创建自己的实用函数来创建您的选项,然后像这样调用 rp()
:
const rp = require('request-promise');
const cheerio = require('cheerio'); // Basically jQuery for node.js
// shared function
function getPage(url) {
const options = {
uri: url,
transform: function(body) {
return cheerio.load(body);
}
};
return rp(options);
}
getPage('https://friendspage.org').then($ => {
// Process html like you would with jQuery...
const nxtPage = $("a[data-url$='nxtPageId']").attr('data');
return getPage(nxtPage).then($ => {
// more processing here
});
}).catch(err => {
console.log(err);
// error handling here
});
这只是将您想在多个地方使用的代码分解为一个共享函数。与 rp()
或 cheerio
没有特别关系,只是 Javascript(或任何语言)中的常规代码分解。