Created
January 21, 2011 23:55
-
-
Save tanepiper/790669 to your computer and use it in GitHub Desktop.
Call the file using node with a keyword to search. `node search-results.js "nodejs rocks"`
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 start = new Date().getTime(); | |
var jsdom = require('jsdom').jsdom; | |
var request = require('request'); | |
var argv = require('optimist') | |
.usage('numresults <word>') | |
.check(function(argv) { if (argv._.length == 0) throw '' }) | |
.argv; | |
var url = 'http://www.google.com/search?hl=en&q=' + encodeURIComponent(argv._[0]); | |
console.log(url); | |
request({uri: url}, function(error, response, body) { | |
var window = jsdom(body.toString(), undefined, { | |
ProcessExternalResources: false, | |
FetchExternalResources: false, | |
}).createWindow(); | |
var $ = require('jquery').create(window); | |
var numResults = $('#resultStats').html().split(' ')[1]; | |
console.log(numResults, 'results for', url); | |
var items = []; | |
var c = 0; | |
$('#ires ol li .l').each(function() { | |
if (c < 10) { | |
items.push( $(this).text() ); | |
c++; | |
} else { | |
return; | |
} | |
}); | |
console.log('Top 10 results'); | |
items.forEach(function(item, i) { | |
console.log('%d %s', i+1, item); | |
}) | |
var end = new Date().getTime(); | |
console.log('Total Time: %d s', (end - start) / 1000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1 chris, I've been looking for something that's a simple package to do scraping with, this was just a wee experiment to test the flexibility of using nodejs + jQuery to do screen scraping. I'll check your package out today