2014-07-21 18 views
6

İlk kez phantomJS'yi deniyorum ve bir siteden bir veriyi başarılı bir şekilde ayıkladım, ancak bir dosyaya bir içerik yazmayı denediğimde hatayı alıyorum: ReferenceError: Can't değişken bulmak: benim senaryomPhantomJS Dosyaya yazma hatası fs. Değişken bulamıyor: fs

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

burada ne İşte

fs dışarı eksik olan?

Düzeltme: Bu soruya verilen yanıtta, neden verilere ulaşamadığımı ve bu bilgilere nasıl erişebileceğimi öğrendim. the docs for page.evaluate() gelen Düz

  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

Dosyaya bazı web sayfası içeriğini nasıl yazabileceğinizi göstermek için bir yanıt ekledim. – Mritunjay

cevap

8

page.evaluate ürününde phantomjs nesneye sahip olamazsınız, çünkü bu bir web sayfasıdır. Size basit bir örnek vereceğim, ne yaptığınızı nasıl elde edersiniz. Bir dosyaya webpage'un bir miktar içeriğini yazmak isterseniz, bu içerikleri page.evaluate'dan iade etmeniz gerekmektedir. ve bu değerleri page.open'da alacaksınız. Burada fs'a erişiminiz var, böylece bu içerikleri yazabilirsiniz.

Basit bir örnekle, bir dosyaya nasıl webpage başlığı yazabileceğinizi gösteriyorum.

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

:

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.

gerekli Başka bir açıklama. o fs göremiyorum böylece

Sizin evaluate() ed fonksiyonu, ancak sayfasında, senin Hayaletler komut bağlamında çalıştırmak değildir: Tomalak en yanıta genişletilmesi

2

.

Bu durumda, işlevinizin betiğinizin sonuçlarını başka bir şekilde okumasını istersiniz.

İlgili konular