【问题标题】:nodejs exec Wget commandnodejs exec wget 命令
【发布时间】:2017-04-03 21:08:59
【问题描述】:

我正在编写一个 nodejs 应用程序,用于使用“wget”unix 命令下载整个网站,但我在下载页面中的一些 url 存在问题,.html 出现在文件末尾,例如

<img src=“images/photo.jpeg.html”> or <script src=“js/scripts.js.html”>

我使用的代码如下:

    var util = require('util'),
    exec = require('child_process').exec,
    child,
    url = 'http://www.example.com/';
child = exec('wget --mirror -p --convert-links --html-extension -e robots=off -P /destination_folder/ ' + url,
  function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);
    }
});

注意,如果我直接在 Unix shell 上使用此命令(wget --mirror -p --html-extension --convert-links -e robots=off -P .http://www.example.com),它可以正常工作。

编辑: 这是运行nodejs脚本后返回的日志:

--2017-04-04 11:49:49--  http://www.example.com/css/style.min.css
Reusing existing connection to www.example.com:80.
HTTP request sent, awaiting response... 304 Not Modified
File ‘/destination_folder/www.example.com/css/style.min.css.html’ not modified on server. Omitting download.

FINISHED --2017-04-04 11:50:11--
Total wall clock time: 22s
Downloaded: 50 files, 1.2M in 1.4s (855 KB/s)
/destination_folder/www.example.com/css/style.min.css.html: No such file or directory
Converting links in /destination_folder/www.example.com/css/style.min.css.html... nothing to do.
exec error: Error: stderr maxBuffer exceeded

我不明白问题出在哪里,请您帮帮我吗?

谢谢

【问题讨论】:

  • 您可能应该更详细地描述您如何确定它不起作用。是否设置了error(如果设置了,消息是什么)?是否有stderr 输出(如果有,它包含什么)?这些都不是这种情况,但您仍然没有在目标目录中看到任何内容?还有什么?
  • Node.JS 执行的 wget 是否与您从命令行执行的版本相同?
  • @tiblu 我觉得是同一个版本,怎么查?
  • @SMadry exec('wget --version') 并在终端中运行相同的操作。
  • @tiblu 相同:1.19.1

标签: node.js wget


【解决方案1】:

exec 使用stdoutsterr 之间的缓冲区,这是有限的。

如果要下载的文件很大,缓冲区可能会用完空间。 尝试使用spawn intestad 或exec。 供你参考: Difference between spawn and exec of Node.js

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多