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
How can I replicate the functionality of a wget with node.js?
提问by Connor
Is it possible to essentially run a wget
from within a node.js app? I'd like to have a script that crawls a site, and downloads a specific file, but the href
of 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 href
of 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 运行外部命令:
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
回答by Wes Johnson
While it might be a little more verbose than some third-party stuff, Node's core HTTP
module 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");
}
});