Download a file using Nightmare

前端 未结 4 1344
时光说笑
时光说笑 2020-12-15 12:25

I am using Nightmare to create a automated downloader for today\'s newspaper. I managed to login and go the the specified page. However I could not find out how to download

4条回答
  •  一生所求
    2020-12-15 12:33

    I got my downloads super easy using the request module, as described here.

    var Nightmare = require('nightmare');
    var fs = require('fs');
    var request = require('request');
    
    new Nightmare()
      .goto('https://login.nrc.nl/login?service=http://digitaleeditie.nrc.nl/welkom')
      .insert('input[name="username"]', 'Username')
      .insert('input[name="password"]','Password')
      .click('button[type="submit"]')
      .wait()
      .goto('http://digitaleeditie.nrc.nl/digitaleeditie/NH/2014/10/20141124___/downloads.html')
      .wait()
      .then(function () {
        download('http://digitaleeditie.nrc.nl/digitaleeditie/helekrant/epub/nrc_20141124.epub', 'myBook.epub', function () {
          console.log('done');
        });
      })
      .catch(function (err) {
        console.log(err);
      })
    
    function download(uri, filename, callback) {
      request.head(uri, function () {
        request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
      });
    }
    

    Run npm i request in order to use request.

提交回复
热议问题