2013-09-22 37 views

回答

2

为了简单起见,查看CasperJS作为一种方法来获取数据&在另一种语言后处理。我会选择#1选项 - 以JSON格式获取数据,并将其保存到文件中以便稍后执行。

要做到这一点,您可以使用PhantomJS提供的File System API。您也可以将它与CasperJS's cli interface耦合以允许您将参数传递到脚本中(例如,要写入的临时文件)。

你的脚本来处理这一切看起来像:

  1. 获得(在Linux系统上mktemp)临时文件的路径。
  2. 调用您的CasperJS脚本,将该临时文件路径作为参数传入。
  3. 获取您的数据,使用File System API将其写入该文件,然后退出。
  4. 阅读文件,使用它(保存到数据库等),删除临时文件。
5

我只是用第二种情况:

第一:获得存储在globalInfo变量的信息

var globalInfo; 
casper.thenOpen("www.targetpage.cl/valuableInfo", function() { 
    globalInfo = this.evaluate(function(){ 
     var domInfo = {}; 
     domInfo.title = "this is the info"; 
     domInfo.body = "scrap in the dom for info"; 
     return domInfo; 
    }); 
}); 

二:访问的网页存储所捕获的数据

casper.then(function(){ 
    casper.thenOpen("www.mipage.com/saveIntheDBonPost.php", { 
     method: 'post', 
     data:{    
      'title': ''+globalInfo.title, 
      'body': ''+globalInfo.body 
     } 
    }); 
}); 

www.mipage.com/saveIntheDBonPost.php取参数$_POST中的数据并将其存储到数据库。