[英]How can I replicate the functionality of a wget with node.js?
是否有可能從node.js應用程序中運行wget
? 我想要一個抓取網站的腳本,然后下載一個特定的文件,但是文件鏈接的href
會經常發生變化。 因此,我認為最簡單的方法是找到鏈接的href
,然后只需對它執行wget即可。
謝謝!
但是為了將來參考,我建議使用request ,這樣可以很容易地獲取該文件:
var request = require("request");
request(url, function(err, res, body) {
// Do funky stuff with body
});
雖然它可能比某些第三方內容更冗長,但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);
});
您可以使用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);
}
});
您可以使用node-wget 。 適用於無法“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");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.