Skip to content

Instantly share code, notes, and snippets.

@mvsusp
Created May 17, 2015 13:01
Show Gist options
  • Save mvsusp/cc3229a8803af5f6e310 to your computer and use it in GitHub Desktop.
Save mvsusp/cc3229a8803af5f6e310 to your computer and use it in GitHub Desktop.
motor js
var express = require('express');
var router = express.Router();
router.get('/crawler', function(req, res, next) {
var elements,
data,
util = require('util'),
spawn = require('child_process').spawn,
ls = spawn('phantomjs', ['motor.js', 'http://www.medium.com']);
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
var data = ls.stdout.pipe(res);
});
module.exports = router;
var page = require('webpage').create();
var system = require('system');
var url = system.args[1];
page.open(url, function(status) {
var elements = page.evaluate(function() {
var nodes = {};
[].forEach.call(document.querySelectorAll('*'), function(element) {
var computedStyle = getComputedStyle(element);
var nodeStyle = {};
for(var i = 0; i < computedStyle.length; i++){
var node = computedStyle[i];
nodeStyle[node] = computedStyle.getPropertyValue(node);
}
if (nodes[element.nodeName]) {
nodes[element.nodeName].push(nodeStyle);
} else {
nodes[element.nodeName] = [nodeStyle];
}
});
return nodes;
});
console.log(JSON.stringify(elements));
phantom.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment