Skip to content

Instantly share code, notes, and snippets.

@tanepiper
Created January 21, 2011 23:55
Show Gist options
  • Save tanepiper/790669 to your computer and use it in GitHub Desktop.
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"`
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);
});
@chriso
Copy link

chriso commented Jan 22, 2011

https://github.com/chriso/node.io - so much nicer for scraping.

@tanepiper
Copy link
Author

+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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment