我正在使用Nightmare为今天的报纸创建一个自动下载器。我设法登录并转到指定的页面。但是我无法找到如何使用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('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!');
});
我尝试通过单击下载按钮下载文件。然而,这似乎不起作用。
答案 0 :(得分:5)
所以你需要交换
.click('a[href="/digitaleeditie/helekrant/epub/nrc_20141124.epub"]')
代表
.evaluate(function ev(){
var el = document.querySelector("[href*='nrc_20141124.epub']");
var xhr = new XMLHttpRequest();
xhr.open("GET", el.href, false);
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.send();
return xhr.responseText;
}, function cb(data){
var fs = require("fs");
fs.writeFileSync("book.epub", data, "binary");
})
您还可以使用更新的方式请求二进制数据。
.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");
})
答案 1 :(得分:2)
有一个Nightmare download plugin。 您可以使用以下代码下载该文件:
var Nightmare = require('nightmare');
require('nightmare-download-manager')(Nightmare);
var nightmare = Nightmare();
nightmare.on('download', function(state, downloadItem){
if(state == 'started'){
nightmare.emit('download', '/some/path/file.zip', downloadItem);
}
});
nightmare
.downloadManager()
.goto('https://github.com/segmentio/nightmare')
.click('a[href="/segmentio/nightmare/archive/master.zip"]')
.waitDownloadsComplete()
.then(() => {
console.log('done');
});
答案 2 :(得分:1)
我使用request
module轻松了解我的下载内容,如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);
});
}
运行npm i request
以使用request
。
答案 3 :(得分:0)
如果点击下载链接,Nightmare会正确下载。
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));