PhantomJS Problem writing to fs file. Unable to find variable: fs

I am trying to run phantomJS for the first time, and I have successfully extracted som data from the site, but when I try to write some content to a file, I get an error: ReferenceError: I can not find the variable: fs

Here is my script

var page = require('webpage').create(); var fs = require('fs'); page.onConsoleMessage = function(msg) { console.log(msg); }; page.open("http://www.pinterest.com/search/pins/?q=motorbike", function(status) { if (status === "success") { page.includeJs("http://code.jquery.com/jquery-latest.js", function() { page.evaluate(function() { var imgs = { title: [], href: [], ext: [], src: [], alt: [] }; $('a.pinImageWrapper').each(function() { imgs.title.push($(this).attr('title')); imgs.href.push($(this).attr('href')); var ext = $(this).children('.pinDomain').html(); imgs.ext.push(ext); var img = $(this).children('.fadeContainer').children('img.pinImg'); imgs.src.push(img.attr('src')); imgs.alt.push(img.attr('alt')); }); if (imgs.title.length >= 1) { for (var i = 0; i < imgs.title.length; i++) { console.log(imgs.title[i]); console.log(imgs.href[i]); console.log(imgs.ext[i]); console.log(imgs.src[i]); console.log(imgs.alt[i]); } } else { console.log('No pins found'); } fs.write('foo.txt', 'bar'); }); phantom.exit(); }); } }); 

What am I missing here?

Edit: From the answer to this question, I found out why I could not reach the data inside evalute and how I could access it.

  var page = require('webpage').create(); var fs = require('fs'); page.onConsoleMessage = function(msg) { console.log(msg); }; openPinPage('motorbike'); function openPinPage(keyword) { page.open("http://www.pinterest.com/search/pins/?q=" + keyword, function(status) { if (status === "success") { page.includeJs("http://code.jquery.com/jquery-latest.js", function() { getImgsData(); }); } }); } function getImgsData() { var data = page.evaluate(function() { var imgs = { title: [], href: [], ext: [], src: [], alt: [] }; $('a.pinImageWrapper').each(function() { imgs.title.push($(this).attr('title')); imgs.href.push($(this).attr('href')); var ext = $(this).children('.pinDomain').html(); imgs.ext.push(ext); var img = $(this).children('.fadeContainer').children('img.pinImg'); imgs.src.push(img.attr('src')); imgs.alt.push(img.attr('alt')); }); return imgs; }); for (var i = 0; i < data.title.length; i++) { console.log(data.title[i]); }; phantom.exit(); } 
+6
source share
3 answers

You cannot have phantomjs objects in page.evaluate because it is a web page. I will give you a simple example of how you can achieve what you are doing.

If you want to write some webpage content in a file, you must return those from page.evaluate . and you will get these values ​​in page.open . Here you have access to fs , so you can write this content.

I show with a simple example how you can write a webpage header to a file.

 page.open("http://www.pinterest.com/search/pins/?q=motorbike", function(status) { if (status === "success") { page.includeJs("http://code.jquery.com/jquery-latest.js", function() { var title = page.evaluate(function() { return document.title; // here I don't have access to fs I'll return title of document from here. }); console.log(title) //I got the title now I can write here. fs.write('foo.txt', title); phantom.exit(); }); } }); 
+8
source

Right from the docs for page.evaluate() :

Computes this function in the context of a web page. execution is isolated, the web page does not have access to the phantom object, and it cannot examine its own settings.

No further explanation is required.

+3
source

Extension of Tomalak's answer:

Your evaluate() ed function does not run in the context of your Phantoms script, but on the page, so it cannot see fs .

In this case, you want your function to read the results of your script in some other way.

+2
source

Source: https://habr.com/ru/post/972584/


All Articles