javascript 如何使用 node.js 复制 wget 的功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9541177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:01:12  来源:igfitidea点击:

How can I replicate the functionality of a wget with node.js?

javascriptnode.jswget

提问by Connor

Is it possible to essentially run a wgetfrom within a node.js app? I'd like to have a script that crawls a site, and downloads a specific file, but the hrefof the link that goes the file changes fairly often. So, I figured the easiest way to go about doing it would be to find the hrefof the link, then just perform a wget on it.

是否可以wget从 node.js 应用程序中运行 a ?我想要一个脚本来抓取一个站点,并下载一个特定的文件,但是文件href的链接经常发生变化。所以,我认为最简单的方法是找到href链接的 ,然后对其执行 wget 。

Thanks!

谢谢!

采纳答案by mrwooster

You can run an external command using child_processes:

您可以使用 child_processes 运行外部命令:

http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_exec_command_options_callback

http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_exec_command_options_callback

var util = require('util'),
    exec = require('child_process').exec,
    child,
    url = 'url to file';

child = exec('wget ' + url,
  function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);
    }
});

回答by Linus Gustav Larsson Thiel

For future reference though, I would recommend request, which makes it this easy to fetch that file:

不过,为了将来参考,我建议使用request,这样可以轻松获取该文件:

var request = require("request");

request(url, function(err, res, body) {
  // Do funky stuff with body
});

回答by Wes Johnson

While it might be a little more verbose than some third-party stuff, Node's core HTTPmodule provides for an HTTP clientyou could use for this:

虽然它可能比一些第三方的东西更冗长,但 Node 的核心HTTP模块提供了一个HTTP 客户端,你可以使用它:

var http = require('http');
var options = {
    host: 'www.site2scrape.com',
    port: 80,
    path: '/page/scrape_me.html'
  };
var req = http.get(options, function(response) {
  // handle the response
  var res_data = '';
  response.on('data', function(chunk) {
    res_data += chunk;
  });
  response.on('end', function() {
    console.log(res_data);
  });
});
req.on('error', function(err) {
  console.log("Request error: " + err.message);
});

回答by j_mcnally

U can just use wget.

你可以只使用wget。

var exec = require('child_process').exec;

child = exec("/path/to/wget http://some.domain/some.file", function (error, stdout, stderr) {
if (error !== null) {
  console.log("ERROR: " + error);
}
else {
  console.log("YEAH IT WORKED");
}
});

回答by Stan Onyime

You can use node-wget. Works in cases where 'wget' is not possible

您可以使用node-wget。在“wget”不可能的情况下工作