網頁爬蟲小程式

到信義房屋網站爬房價

利用一些套件抓回整個網頁,再用JQuery CSS選擇器截取需要的資訊

var request = require("request");
var cheerio = require("cheerio");

var options = { method: 'GET',
  url: 'http://tradeinfo.sinyi.com.tw/community/communityDetail.html?c=0011520',
  qs: { types: '1' },
  headers: 
   { 'postman-token': 'b35040aa-76ee-c26e-e72c-611100189cec',
     'User-Agent': 'request',
     'cache-control': 'no-cache' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);
  //console.log(body);
 
  var $ = cheerio.load(body);
  var result = [];
  var objName = $(".suggest .objname");
  var objLeft = $(".suggest .left");
  var objPrice = $(".suggest .wcolor");
  var objSuggestW = $("[class='right suggestW']");
  
  for(var i=0;i<objLeft.length;i++) {
    var myhouse = new Object()
    myhouse.物件 = $(objName[i]).text();
    myhouse.坪數 = $(objLeft[i]).text();
    myhouse.總價 = $(objPrice[i]).text();
    myhouse.格局 = $(objSuggestW[i]).text();
    
    result.push(myhouse);
  }
  
  console.log(result);
})