LoginSignup
2
2

More than 5 years have passed since last update.

Sails.jsとPhantomJSを使ったスクレイピングをする準備をする

Last updated at Posted at 2015-04-21

したこと

  • SailsでとりあえずPhantomJSでページをとってみた。
# sails new landload
# sails generate api property
# npm install phantomjs
config/models.js
module.exports.models = {
    migrate: 'safe'
};
controllers/PropertyController.js
var phantom = require('phantom');

function CreateUrlArray(str) {
  var array = [];

  str.match(/.*(\[(\d+)-(\d+)\]).*/);

  if (!RegExp.$1 || !RegExp.$2 || !RegExp.$3) {
    return array;
  }
  else {
    var replace = RegExp.$1;
    var start = Number(RegExp.$2);
    var end = Number(RegExp.$3);

    for (var i = 0; i <= end - start; i++) {
      array[i] = str.replace(replace, (start + i).toString());
    }
    return array;
  }
};

function updateApi(req, res) {
  phantom.create(function (ph) {
    ph.createPage(function (page) {
      page.open("http://www.rakumachi.jp/syuuekibukken/area/prefecture/dimAll/?page=1&limit=100", function (status) {
        console.log("opened rakumachi? ", status);
        page.evaluate(function () {
          return document.body.textContent;
        }, function (result) {
          res.json(200, {result: result});
          ph.exit();
        });
      });
    });
  });
};

var PropertyController = {
  update: updateApi
};

module.exports = PropertyController;
models/Property.js
module.exports = {
  attributes: {
  }
};

はまったこと

  • 最新の2.0系列のPhantomJSはまだnpmからは取れないので、古い1.9系列を使うことにした。
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2