Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 601
Connor Avatar asked Mar 02 '12 22:03

Connor


People also ask

Which method is used to read files on your computer in Node JS?

The fs.readFile() method is used to read files on your computer.

What is Dispatch in node JS?

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.

How does NodeJS work behind the scenes?

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.


2 Answers

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
});
like image 90
Linus Thiel Avatar answered Oct 14 '22 13:10

Linus Thiel


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);
});
like image 31
Wes Johnson Avatar answered Oct 14 '22 11:10

Wes Johnson