简体   繁体   English

如何从Zombie.js流/管道响应对象

[英]How to stream/pipe response object from Zombie.js

How can I download a file with Zombie.js with a web app that uses http-equiv meta tag to emulate the Refresh HTTP header? 如何使用Zombie.js下载带有使用http-equiv元标记模拟Refresh HTTP标头的Web应用程序的文件?

In essence how do you download/stream/pipe a downloadable resource that is loaded after sometime have passed and not via a direct download link. 本质上,如何下载/流/管道在经过一段时间后加载的可下载资源,而不是通过直接下载链接。

I was thinking I could do it with the Zombie.js Pipeline... but the docs on that are pretty senseless to a zombiejs newbie 我以为我可以用Zombie.js Pipeline做到这一点......但对于zombiejs新手而言,这些文档对我来说是毫无意义的

Not sure if this would help you, but this helped me catch response bodies. 不确定这是否会对你有所帮助,但这有助于我找到回复机构。 (Copied from a GitHub issue .) (从GitHub问题复制。)

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

Can be used to download BINARY FILES 可以用来下载BINARY FILES

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM