Created
January 18, 2017 19:05
-
-
Save mikeptweet/f94fa2d029e8b61fe7cee4479515b0cf to your computer and use it in GitHub Desktop.
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
var args= require('system').args; | |
var webPage = require('webpage'); | |
var page = webPage.create(); | |
if(args.length <3) { | |
console.log("USAGE: phantomjs.exe scrapepage.js url sql\n"); | |
console.log('ie.: phantomjs.exe scrapepage.js "https://news.ycombinator.com/news" "select nodeName,href,innerText from document where className=\'storylink\'"'); | |
phantom.exit(); | |
} | |
var url= args[1]; | |
var sql=args[2]; | |
page.open(url, function(status) { | |
page.includeJs("https://cdn.jsdelivr.net/g/[email protected](xlsx.core.min.js),[email protected]",function(){ | |
var result=page.evaluate(function(sql) { | |
var response=""; | |
try { | |
alasql("create table document"); | |
alasql.tables.document.data=[].slice.call(document.all); | |
response=alasql(sql); | |
} catch(e) { | |
response=e.message; | |
} | |
return response; | |
},sql); | |
result = (typeof result == 'object') ? JSON.stringify(result,null,2) : result; | |
console.log(result); | |
phantom.exit(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment