使用 NodeJS 和 Cheerio 减少冗余
Reducing Redundancy with NodeJS and Cheerio
我只是想知道如何减少这两个网络抓取工具的冗余,因为我不希望它请求网站两次。我对此很陌生,对语法不是很熟悉。这是代码片段:
request(website_url, function(err, resp, body) {
var $ = cheerio.load(body);
$('.title').each(function(){
var title = $(this).children('h2').children('span').text();
titles.push(title);
});
request(website_url, function(err, resp, body) {
var $ = cheerio.load(body);
$('.post-box-excerpt').each(function(){
var caption = $(this).children('p').text();
captions.push(caption);
});
最简单的方法就是使用一次调用 api:
request(website_url, function (err, resp, body) {
var $ = cheerio.load(body);
$('.title').each(function () {
var title = $(this).children('h2').children('span').text();
titles.push(title);
});
$('.post-box-excerpt').each(function () {
var caption = $(this).children('p').text();
captions.push(caption);
});
});
我只是想知道如何减少这两个网络抓取工具的冗余,因为我不希望它请求网站两次。我对此很陌生,对语法不是很熟悉。这是代码片段:
request(website_url, function(err, resp, body) {
var $ = cheerio.load(body);
$('.title').each(function(){
var title = $(this).children('h2').children('span').text();
titles.push(title);
});
request(website_url, function(err, resp, body) {
var $ = cheerio.load(body);
$('.post-box-excerpt').each(function(){
var caption = $(this).children('p').text();
captions.push(caption);
});
最简单的方法就是使用一次调用 api:
request(website_url, function (err, resp, body) {
var $ = cheerio.load(body);
$('.title').each(function () {
var title = $(this).children('h2').children('span').text();
titles.push(title);
});
$('.post-box-excerpt').each(function () {
var caption = $(this).children('p').text();
captions.push(caption);
});
});