保存使用casperjs抓取网页时获得的表格数据

SG_*_*SG_ 3 javascript mysql json phantomjs casperjs

哪个是保存使用casperjs抓取网页时获得的表数据的最佳方法?

  1. 使用json对象并在序列化后将其存储为文件.

  2. 使用ajax请求到php然后将其存储在mysql数据库中.

Ale*_*lva 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;
   });
});
Run Code Online (Sandbox Code Playgroud)

第二:访问页面以存储捕获的数据

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

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