Created
December 15, 2013 18:24
-
-
Save jpouellet/7976337 to your computer and use it in GitHub Desktop.
Gets the first paragraph of a wikipedia query. I'm sure there's a better way.
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
#!/usr/bin/env phantomjs | |
var system = require('system'); | |
// because console.error currently goes to stdout | |
console.error = system.stderr.writeLine; | |
if (system.args.length !== 2) { | |
console.error('usage: ' + system.args[0] + ' query'); | |
phantom.exit(1); | |
} | |
var url = 'https://en.wikipedia.org/w/index.php?search=' + system.args[1]; | |
function load(url) { | |
var page = require('webpage').create(); | |
var redirectURL = null; | |
page.onResourceReceived = function(resource) { | |
if (url == resource.url && resource.redirectURL) { | |
redirectURL = resource.redirectURL; | |
} | |
}; | |
page.open(url, function(status) { | |
if (redirectURL) { | |
load(redirectURL); | |
} else if (status === 'success') { | |
var para1 = this.evaluate(function() { | |
var c = document.getElementById('mw-content-text').children; | |
for (var i = 0; i < c.length; i++) { | |
if (c[i].tagName != 'P') | |
continue; | |
return c[i].textContent; | |
} | |
return ""; | |
}); | |
console.log(para1); | |
phantom.exit(0); | |
} else { | |
console.error('failed to load ' + url); | |
phantom.exit(2); | |
} | |
}); | |
} | |
load(url); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment