2014-07-21 2 views
6

मैं पहली बार phantomJS को आजमा रहा हूं और मैंने साइट से कुछ डेटा सफलतापूर्वक निकाला है, लेकिन जब मैं किसी फ़ाइल में कुछ सामग्री लिखने का प्रयास करता हूं तो मुझे त्रुटि मिलती है: संदर्भ त्रुटि: नहीं कर सकता वेरिएबल प्राप्त हो: FSFantomJS फ़ाइल fs फ़ाइल लिखने के लिए। चर नहीं मिल सकता: 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(); 
      }); 
     } 
    }); 

क्या मैं यहाँ याद आ रही है है?

संपादित करें: इस प्रश्न पर प्रतिक्रिया के बारे में मैंने सीखा कि मैं evalute के अंदर डेटा तक क्यों नहीं पहुंच पा रहा था, और मैं इसे कैसे एक्सेस कर सकता था।

  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

मैंने यह दिखाने के लिए एक जवाब जोड़ा है कि आप फ़ाइल में कुछ वेबपृष्ठ सामग्री कैसे लिख सकते हैं। – Mritunjay

उत्तर

8

आपके पास phantomjspage.evaluate में ऑब्जेक्ट्स नहीं हो सकते क्योंकि यह एक वेबपृष्ठ है। मैं आपको एक साधारण उदाहरण दूंगा कि आप जो कर रहे हैं उसे कैसे प्राप्त कर सकते हैं।

यदि आप फ़ाइल में webpage की कुछ सामग्री लिखना चाहते हैं तो आपको page.evaluate से उन contens को वापस करना होगा। और आप उन मानों को page.open में प्राप्त करेंगे। यहां आपके पास fs तक पहुंच है, इसलिए आप उन सामग्री को लिख सकते हैं।

मैं एक साधारण उदाहरण के साथ दिखा रहा हूं कि आप फ़ाइल में कुछ webpage शीर्षक कैसे लिख सकते हैं।

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
सीधे 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.

आगे कोई स्पष्टीकरण की जरूरत है।

2

Tomalak के जवाब पर विस्तार:

आपका evaluate() एड समारोह अपने फैंटम स्क्रिप्ट के संदर्भ में नहीं चला है, लेकिन पेज में, तो यह fs नहीं देख सकता।

इस मामले में, आप चाहते हैं कि आपका कार्य किसी अन्य तरीके से आपकी स्क्रिप्ट के परिणाम पढ़े।

संबंधित मुद्दे