【问题标题】:Data prints out multiple times instead of once数据打印多次而不是一次
【发布时间】:2017-12-12 13:23:51
【问题描述】:

我在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

【问题讨论】:

  • "res.on" 被调用了 5 次。

标签: javascript arrays node.js cheerio


【解决方案1】:

您必须同时收听 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]
  );
})

【讨论】:

  • 明白。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2019-03-15
  • 2021-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-06
  • 1970-01-01
相关资源
最近更新 更多