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.
Thanks!
The fs.readFile() method is used to read files on your computer.
It facilitates interaction between objects in Node. A Dispatcher is a service object that is used to ensure that the Event is passed to all relevant Listeners.
Nodejs is a Javascript runtime based on the google v8 engine and that's why it appears here as a dependency and the v8 engine enables Nodejs to understand the javascript code that we write. The v8 engine is what converts javascript into a machine language that the computer can understand.
For future reference though, I would recommend request, which makes it this easy to fetch that file:
var request = require("request");
request(url, function(err, res, body) {
// Do funky stuff with body
});
While it might be a little more verbose than some third-party stuff, Node's core HTTP
module provides for an HTTP client you could use for this:
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With