如何 stream/pipe 从 Zombie.js 响应 object

How to stream/pipe response object from Zombie.js

如何使用使用 http-equiv 元标记模拟刷新 HTTP header 的 Web 应用程序下载带有 Zombie.js 的文件?

本质上,您如何 download/stream/pipe 在一段时间后加载的可下载资源,而不是通过直接下载 link。

我在想我可以用 Zombie.js 管道来做到这一点......但是关于它的文档对 zombiejs 新手来说毫无意义

不确定这是否对您有帮助,但这帮助我找到了响应主体。 (从 GitHub issue 复制而来。)

var Promise = require('bluebird');
const Browser = require('zombie');
const browser = new Browser();


browser.visit(myUrl, function() {
  browser.click('body')
    .then(function(){
      return Promise.map(browser.resources, function(resource){
        return (resource.response._bodyUsed)
          ? resource.response.body
          : resource.response.text();
      }).then(function(bodies){
        console.log(bodies);
      });
    });
});

可用于下载二进制文件

var fs = require('fs');
var URL = require('url');
var Request = require('request'); // Named 'Request' to prevent conflict with zombie's 'request' object
var Browser = require('zombie'), browser = new Browser();

browser.on('request', function (request) {

  // let's pullover for a download incase we find our request signboard :)
  if (request.url.indexOf('https://matchingString') == 0) {

    console.log('Beginning download in a flash...');

    var 
    parsedURL = URL.parse(request.url), 
    cookies = browser.cookies, 
    cookieHeader = cookies.serialize(parsedURL.hostname, parsedURL.pathname);

    if (cookieHeader) request.headers.append('Cookie', cookieHeader);

    var writeStream = fs.createWriteStream(pathToFileYetToBeWritten);

    writeStream
    .on('error', function(err) {
        console.error(err);
    })
    .on('finish', function() {
        console.log('finished writing file');
    });

    Request({
        method: request.method, 
        uri: request.url, 
        headers: request.headers.toObject(), 
        proxy: browser.proxy, 
        body: request.body, 
        jar: false, 
        followRedirect: false, 
        strictSSL: browser.strictSSL, 
        localAddress: browser.localAddress || 0
    })
    .on('error', function(err) {
        console.error(err);
    })
    .pipe(writeStream);

}

});



browser.visit(url, function () {

browser.wait(6000); // Wait only if you need to

});