Last active
October 1, 2018 14:37
-
-
Save stefanocudini/df3ca00a44331767e4f33f28d0462667 to your computer and use it in GitHub Desktop.
web scraping global javascript vars in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Nightmare = require('nightmare') | |
const nightmare = Nightmare({ | |
// show: true | |
}); | |
var url = process.argv[2] || 'http://labs.easyblog.it/', | |
prop = process.argv[3] || undefined; | |
Object.byString = function(o, s) { | |
if(!s) return o; | |
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties | |
s = s.replace(/^\./, ''); // strip a leading dot | |
var a = s.split('.'); | |
for (var i = 0, n = a.length; i < n; ++i) { | |
var k = a[i]; | |
if (k in o) { | |
o = o[k]; | |
} else { | |
return; | |
} | |
} | |
return o; | |
} | |
nightmare | |
.goto(url) | |
//.wait('#content') | |
.evaluate(function() { | |
return this | |
}) | |
.end() | |
.then(function(window) { | |
var o = Object.byString(window, prop); | |
console.log( JSON.stringify(o,null,4) ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment