2014-07-21 21 views
6

Sto provando per la prima volta phantomJS e ho estratto con successo alcuni dati da un sito, ma quando provo a scrivere del contenuto in un file ottengo l'errore: ReferenceError: Can not trovare variabile: fsPhantomJS Problema di scrittura nel file fs. Impossibile trovare la variabile: fs

qui è il mio scritto

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

che cosa mi manca qui?

Modifica: Della risposta a questa domanda ho imparato perché non potevo raggiungere i dati all'interno della valutazione e come potevo accedervi.

  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(); 
     } 
+1

Ho aggiunto una risposta per mostrare come è possibile scrivere del contenuto di una pagina web in un file. – Mritunjay

risposta

8

Non è possibile avere oggetti phantomjs in page.evaluate perché si tratta di una pagina Web. Ti darò un semplice esempio come puoi ottenere ciò che stai facendo.

Se si desidera scrivere del contenuto di webpage in un file, è necessario restituire tali contendenti da page.evaluate. e otterrete quei valori in page.open. Qui puoi accedere a fs, quindi puoi scrivere quei contenuti.

Sto mostrando con un semplice esempio come è possibile scrivere del titolo webpage in un 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(); 
      }); 
     } 
    }); 
3

Direttamente dal the docs for page.evaluate():

Evaluates the given function in the context of the web page. The execution is sandboxed, the web page has no access to the phantom object and it can't probe its own setting.

Nessuna ulteriore spiegazione necessaria.

2

Ampliando la risposta di Tomalak:

La funzione ndr evaluate() non viene eseguito nel contesto del vostro script Phantoms, ma nella pagina, in modo che non può vedere fs.

In questo caso, si desidera che la funzione di leggere i risultati del proprio script in qualche altro modo.

Problemi correlati