Javascript 使用“噩梦”下载文件

Javascript 使用“噩梦”下载文件,javascript,download,phantomjs,nightmare,Javascript,Download,Phantomjs,Nightmare,我正在使用噩梦为今天的报纸创建一个自动下载程序。我设法登录并转到指定页面。然而,我找不到如何下载带有噩梦的文件 var Nightmare = require('nightmare'); new Nightmare() .goto('https://login.nrc.nl/login?service=http://digitaleeditie.nrc.nl/welkom') .type('input[name="username"]', 'Username') .type(

我正在使用噩梦为今天的报纸创建一个自动下载程序。我设法登录并转到指定页面。然而,我找不到如何下载带有噩梦的文件

var Nightmare = require('nightmare');
new Nightmare()
  .goto('https://login.nrc.nl/login?service=http://digitaleeditie.nrc.nl/welkom')
    .type('input[name="username"]', 'Username')
    .type('input[name="password"]','Password')
    .click('button[type="submit"]')
    .wait()
    .goto('http://digitaleeditie.nrc.nl/digitaleeditie/NH/2014/10/20141124___/downloads.html')
    .wait()
    .click('a[href="/digitaleeditie/helekrant/epub/nrc_20141124.epub"]')
    .wait()

    .url(function(url) {
        console.log(url)
    })
    .run(function (err, nightmare) {
      if (err) return console.log(err);
      console.log('Done!');
    });
我试图通过点击下载按钮来下载文件。然而,这似乎不起作用。

当你点击应该下载的东西时,PhantomJS(以及CasperJS和噩梦)不会触发下载(对话框)。所以,有必要自己下载。如果可以找到文件的URL,那么可以使用XMLHttpRequest从页面上下文轻松下载该文件

所以你需要交换

.click('a[href="/digitaleeditie/helekrant/epub/nrc_20141124.epub"]')
为了

您还可以使用更新的方式请求二进制数据

.evaluate(function ev(){
    var el = document.querySelector("[href*='.pdf']");
    var xhr = new XMLHttpRequest();
    xhr.open("GET", el.href, false);
    xhr.responseType = "arraybuffer";
    xhr.send();

    var bytes = [];
    var array = new Uint8Array(xhr.response);
    for (var i = 0; i < array.length; i++) {
        bytes[i] = array[i];
    }
    return bytes;
}, function cb(data){
    var fs = require("fs");
    fs.writeFileSync("book.epub", new Buffer(data), "binary");
})
.evaluate(函数ev(){
var el=document.querySelector(“[href*='.pdf']”);
var xhr=new XMLHttpRequest();
xhr.open(“GET”,el.href,false);
xhr.responseType=“arraybuffer”;
xhr.send();
var字节=[];
var数组=新的Uint8Array(xhr.response);
对于(var i=0;i

描述了这两种方法。是一个示例脚本,它显示了概念验证。

如前所述,我使用获得的下载非常简单

运行
npm i request
以使用
request

有一个。 您可以使用以下代码下载该文件:

var噩梦=require(“噩梦”);
要求('噩梦下载管理器')(噩梦);
var噩梦=噩梦();
on('download',函数(状态,downloadItem){
如果(状态==“已启动”){
emit('download','some/path/file.zip',downloadItem);
}
});
噩梦
.downloadManager()
后藤先生('https://github.com/segmentio/nightmare')
。单击('a[href=“/segmentio/噩梦/archive/master.zip”]”)
.waitDownloadsComplete()
.然后(()=>{
console.log('done');

});如果单击下载链接,噩梦将正确下载

const Nightmare         = require('nightmare');
const show              = ( process.argv[2].includes("true") ) ? true : false;
const nightmare         = Nightmare( { show: show } );

nightmare
    .goto("https://github.com/segmentio/nightmare")
    .click('a[href="/segmentio/nightmare/archive/master.zip"]')
    .end(() => "Done!")
    .then((value) => console.log(value));

我试图实现这一点。但是,这只下载同名的4k文件。它不下载整个文件。4k有点随意。内容是什么?可能这是一个错误页面。它是一个4k大小的epub文件。如果在文本编辑器中打开,它只包含null。您可以用其他方法尝试,看看页面是否支持它。我更新了我的答案。第二个选项抛出一个错误:stream.js:94 thrower;//管道中未处理的流错误。^TypeError:无法在Proto.apply(/home/node\u modules/dream/lib/actions.js:324:14)的wrapped(/home/node\u modules/dream/node\u modules/dream/node\u modules/phantom/node\u modules/dnode/node\u modules/dnode protocol/index.js:123:13)的cb(/home/node/node/node\u modules:32:35)的新缓冲区(Buffer.js.js不确定在调用download()时代码中应该做什么-在参数之间缺少“,”错误
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);
  });
}
const Nightmare         = require('nightmare');
const show              = ( process.argv[2].includes("true") ) ? true : false;
const nightmare         = Nightmare( { show: show } );

nightmare
    .goto("https://github.com/segmentio/nightmare")
    .click('a[href="/segmentio/nightmare/archive/master.zip"]')
    .end(() => "Done!")
    .then((value) => console.log(value));