Created
May 30, 2012 18:01
-
-
Save olsonjeffery/2837988 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Adapted, by Jeff Olson, from: | |
# Scraping Made Easy with jQuery and SelectorGadget | |
# (http://blog.dtrejo.com/scraping-made-easy-with-jquery-and-selectorga) | |
# by David Trejo | |
# | |
# Install node.js and npm: | |
# http://joyeur.com/2010/12/10/installing-node-and-npm/ | |
# Then run | |
# npm install jsdom jquery http-agent | |
# node numresults.js | |
# | |
util = require 'util' | |
url = require 'url' | |
httpAgent = require 'http-agent' | |
jsdom = require('jsdom').jsdom | |
printTop3 = (agent) -> | |
window = jsdom(agent.body).createWindow() | |
$ = require('jquery').create(window) | |
# | |
# Now you can use jQuery to your heart's content! | |
# | |
titles = $('.title a') | |
points = $('.subtext span') | |
printme = $.map points, (el, i) -> | |
if i < 3 | |
return $(el).text() + '\t' + $(titles[i]).text() | |
console.log printme.join('\n') | |
urls = ['', 'newest'] | |
agent = httpAgent.create 'news.ycombinator.com', urls | |
console.log 'Scraping', urls.length, 'pages from', agent.host | |
agent.addListener 'next', (err, agent) -> | |
printTop3 agent | |
console.log() | |
agent.next() | |
agent.addListener 'stop', (err, agent) -> | |
if err then console.log(err) | |
console.log 'All done!' | |
# Start scraping | |
agent.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment