Skip to content

Instantly share code, notes, and snippets.

@laiso
Created March 2, 2011 13:16
Show Gist options
  • Save laiso/850908 to your computer and use it in GitHub Desktop.
Save laiso/850908 to your computer and use it in GitHub Desktop.
/**
* # USAGE
*
* phantomjs ./google_search.js 'javascript'
*
* phantomjs ./google_search.js 'Steven Jobs' > result.csv
*
*/
var BASE_URL = 'http://www.google.co.jp/search?';
phantom.userAgent = 'google_search.js / https://gist.github.com/850908';
if (phantom.state.length == 0) {
phantom.state = 'search';
console.log('"id", "title", "url"');
phantom.open(BASE_URL + '&q=' + phantom.args[0] + '&start=' + '0');
} else {
if (document.querySelector('a#pnnext') == null) {
phantom.exit();
} else {
var url = document.location.search.substring(1);
var cur = parseInt(url.match(/start=(\d+)&?/)[1], 0);
var links = document.querySelectorAll('a.l');
for (var i = 0; i < links.length; i++) {
var id = cur + i;
console.log(id + ',"' + links[i].text + '","' + links[i].href + '"');
}
cur += 10;
phantom.sleep(1000);
phantom.open(BASE_URL + '&q=' + phantom.args[0] + '&start=' + cur);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment