数据打印多次而不是一次

Data prints out multiple times instead of once

我在 node.js 中制作了一个网络抓取应用程序,它从网站的 HTML 中获取 p.titlep.artist,然后将这两个元素中的每一个存储在两个单独的元素中这样的数组:

res.on('data', function(html){
  var $ = cheerio.load(html);
  var commonTitles = [];
  var commonArtists = [];

  commonTitles.push($.text($('p.title').eq(0)));
  commonTitles.push($.text($('p.title').eq(1)));
  commonTitles.push($.text($('p.title').eq(2)));
  commonTitles.push($.text($('p.title').eq(3)));
  commonTitles.push($.text($('p.title').eq(4)));

  commonArtists.push($.text($('p.artist').eq(0)));
  commonArtists.push($.text($('p.artist').eq(1)));
  commonArtists.push($.text($('p.artist').eq(2)));
  commonArtists.push($.text($('p.artist').eq(3)));
  commonArtists.push($.text($('p.artist').eq(4)));

  console.log(
    'The 5 most recently played titles: \n' +
    commonTitles[0] + ' / ' + commonArtists[0] + '\n' +
    commonTitles[1] + ' / ' + commonArtists[1] + '\n' +
    commonTitles[2] + ' / ' + commonArtists[2] + '\n' +
    commonTitles[3] + ' / ' + commonArtists[3] + '\n' +
    commonTitles[4] + ' / ' + commonArtists[4]
  );
});

我想从一个站点列出所有 5 首最近播放的歌曲名称及其艺术家,并在 console.log 的帮助下将它们打印出来。我希望在命令提示符下得到这个作为我的输出:

The 5 most recently played titles:
I Make It Rain (Original Trap Mix) / Jimithegenius
More Pragmatic (Original Version) / Jahnauasca
Kiss The Devil (Just A Gent Remix) / Bel Hair
Check (Levitate Remix) / Meek Mill
Party Right (Charlie Traplin Trap Mix) / Lethal Bizzle feat. Ruby Goe

播放列表不时更新,所以这只是一个例子。
而不是像那样打印出来,而是打印出没有数组的附加参数,就好像 console.log 已经被放入这样一个奇怪的循环中:

The 5 most recently played titles:
/
/
/
/
/
The 5 most recently played titles:
/
/
/
/
/
The 5 most recently played titles:
I Make It Rain (Original Trap Mix) / Jimithegenius
More Pragmatic (Original Version) / Jahnauasca
Kiss The Devil (Just A Gent Remix) / Bel Hair
Check (Levitate Remix) / Meek Mill
Party Right (Charlie Traplin Trap Mix) / Lethal Bizzle feat. Ruby Goe
The 5 most recently played titles:
/
/
/
/
/
The 5 most recently played titles:
/
/
/
/
/

为什么会打印出额外的数据? 如果 console.log 处于循环中,那么所有附加输出都不会为空,它们都将包含数组中的数据。我什至尝试将 res.on 之外的数组创建为全局数组,但随后它将标题和艺术家打印为 undefined。我如何摆脱这些不需要的线条?

提前致谢,
Tim_W8

您必须同时收听 dataend 才能完成这项工作。

data 每次数据块到达时调用。

end 调用全部所有数据到达。

var html = '';
res.on('data', function(chunk){
   html+=chunk;
});

res.on('end',function(){

  var $ = cheerio.load(html);
  var commonTitles = [];
  var commonArtists = [];

  commonTitles.push($.text($('p.title').eq(0)));
  commonTitles.push($.text($('p.title').eq(1)));
  commonTitles.push($.text($('p.title').eq(2)));
  commonTitles.push($.text($('p.title').eq(3)));
  commonTitles.push($.text($('p.title').eq(4)));

  commonArtists.push($.text($('p.artist').eq(0)));
  commonArtists.push($.text($('p.artist').eq(1)));
  commonArtists.push($.text($('p.artist').eq(2)));
  commonArtists.push($.text($('p.artist').eq(3)));
  commonArtists.push($.text($('p.artist').eq(4)));

  console.log(
    'The 5 most recently played titles: \n' +
    commonTitles[0] + ' / ' + commonArtists[0] + '\n' +
    commonTitles[1] + ' / ' + commonArtists[1] + '\n' +
    commonTitles[2] + ' / ' + commonArtists[2] + '\n' +
    commonTitles[3] + ' / ' + commonArtists[3] + '\n' +
    commonTitles[4] + ' / ' + commonArtists[4]
  );
})