我正在使用 Cheerio js 抓取一个网站,我有一个数组中的表格行列表,当我在 for 循环中使用 $.html('text') 修改内部 HTML 的内容时似乎工作但一旦函数退出我丢失了修改后的文本:
var cheerio = require('cheerio');
var bands = [];
var res = function (data) {
for (var j=0; j < data.length; j++) {
var perline = data[j];
var $ = cheerio.load(perline);
var chline = $('[class^="eventSlot"]');
for (var i=0; i < chline.length; i++) {
console.log($(chline[i]).html()); // looks correct
$(chline[i]).html('some text'); // modify inner HTML
console.log($(chline[i]).html()); // looks modified
}
bands.push(perline);
}
return bands;
};
var html = ['<td>11/04/2014</td><td><span class="eventSlot slot1 headliner">Band1</span><span class="eventSlot slot2">Band2</span></td><td>',
'<td>11/04/2014</td><td><span class="eventSlot slot1 headliner">Band3</span></td>'];
console.log(res(html)); // contents are not modified from original html