2014-07-21 97 views
6

我第一次尝试了phantomJS,并且我已经成功地从站点提取了som数据,但是当我尝试向文件写入一些内容时,出现错误:ReferenceError:Can not找到变量:FSPhantomJS写入文件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(); 
      }); 
     } 
    }); 

缺少什么我在这里?

编辑:在这个问题的答案我知道为什么我无法达到评估内的数据,以及我如何访问它。

  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

您不能在page.evaluate中有phantomjs对象,因为这是一个网页。我会给你一个简单的例子,你怎么能够实现你在做什么。

如果你想在文件中写入一些webpage的内容,你必须从page.evaluate返回这些比赛。你会得到这些值在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

扩展在托默勒格的回答是:

evaluate()编辑功能没有在你的幽灵脚本的上下文中运行,但在页面中,所以它不能看到fs

在这种情况下,您会希望函数以其他方式读取脚本的结果。