How to save the downloadable file link in zombie.js

201 Views Asked by At

I am scrapping a website using node.js and zombie.js. I am facing an issue where in a file I have an anchor which holds the link to download a pdf file. If I click it using browser.clickLink() function, the result that I get in console is beyond my understanding. Is there a way to save this pdf file and have its link like in php? I want to save it for further processing. Here is my test js code

var http = require('http');
var browser = require('zombie');
var assert = require('assert'); 

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  //res.setHeader('Content-Type', 'text/plain');
  //res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
  
});

var url = 'http://localhost/Node/HM_LandRegistry/downloadPdf.html'

browser.visit(url, function(error,browser) {
  
  
  //browser.dump();
 //console.log('browser.text (".link")', browser.text(".link")); 
 browser.clickLink("a.link");
  browser.wait().then(function(){
     console.log(browser.text());
     browser.dump();
  });
});

1

There are 1 best solutions below

0
umair.ashfr On BEST ANSWER

Here is something I found on google groups. It has solved my problem.

function getLinks(browser) {
 var links = browser.querySelectorAll('.link');
 return Array.prototype.map.call(links, function(e) {
 return e.getAttribute('href'); // returns an array. Use .toString() to get string only
});
}

Save the link