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

后端 未结 5 1929
我寻月下人不归
我寻月下人不归 2021-02-02 17:01

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

5条回答
  •  情书的邮戳
    2021-02-02 17:20

    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);
    });
    

提交回复
热议问题