简体   繁体   English

wget的Node.js执行

[英]Node.js exec with wget

I'm trying to download a lot of files using nodejs and the exec command, simplified like this: 我正在尝试使用nodejs和exec命令下载很多文件,简化如下:

var cmd = 'wget -O output.csv URL';
var child = exec(cmd, function(err) {
  console.log('DONE');
});

However, the callback is triggered before the file was actually downloaded through wget , leading to a file that contains garbage like ' 0O 6D 1n ]v # '. 但是,回调是在实际通过wget下载文件之前触发的,从而导致文件包含诸如``0O6D1n1] v。 Shouldn't the callback be triggered once wget is done? wget完成后是否应该触发回调? When running the same command on the command line it takes rougly 5 seconds, since the file has several MB. 在命令行上运行同一命令时,该文件有几个MB,因此要花5秒钟的时间。

Btw: I'm not using the request module since it's slower and I ran into emitter listener issues ( EventEmitter memory leak detected. 11 listeners added ). 顺便说一句:我不使用request模块,因为它速度较慢,并且遇到了发射器侦听器问题( EventEmitter memory leak detected. 11 listeners added )。

Thanks! 谢谢!

This will involve some debugging. 这将涉及一些调试。

Can you please try running your script as: 您能否以下列方式尝试运行脚本:

var cmd = 'wget -O output.csv URL';
var child = exec(
  cmd,
  function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);
    }
  }
);

It would be interesting to see what stdout and stderr say. 看看stdout和stderr怎么说。

Right, you provided me your stderr which said: 正确,您向我提供了stderr ,其中说:

http://productdata.zanox.com/exportservice/v1/rest/22791753C32335607.csv?ticket=BC4B91472561713FD43BA766542E9240AFDD01B95B123E40B2C0375E3A68C142

This URL the command line gets is missing everything after the ampersand ( & character). 命令行获取的此URL缺少与号( &字符)之后的所有内容。 This indicates a problem with escaping. 这表明转义有问题。

To get around this try replacing \\& with \\\\\\& . 要解决此问题,请尝试将\\&替换为\\\\\\&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM