Created
April 2, 2016 23:34
-
-
Save frenchie4111/12f8a43afec264a1efd5f755e29aaeb6 to your computer and use it in GitHub Desktop.
Example Node (4) script that pulls posts from the front page of Hacker News
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
/** | |
* Copyright (C) 2016 Michael Lyons | |
* | |
* This software may be modified and distributed under the terms | |
* of the MIT license. | |
*/ | |
var _ = require( 'underscore' ), | |
q = require( 'q' ), | |
request = require( 'request' ), | |
cheerio = require( 'cheerio' ); | |
var get = function( url ) { | |
return q | |
.Promise( ( resolve, reject ) => { | |
request( { | |
url: url, | |
method: 'get' | |
}, ( err, res, body ) => { | |
if( err ) return reject( err ); | |
resolve( body ); | |
} ); | |
} ); | |
}; | |
q | |
.async( function *() { | |
var page_html = yield get( 'http://news.ycombinator.com' ); | |
var $ = cheerio.load( page_html ); | |
var results = $( '.athing' ) | |
.find( '.title>a' ); | |
return _ | |
.map( results, ( elm ) => { | |
return { | |
text: $( elm ).text(), | |
href: $( elm ).attr( 'href' ) | |
}; | |
} ) | |
} )() | |
.then( ( results ) => { | |
console.log( results ); | |
process.exit( 0 ); | |
} ) | |
.catch( ( err ) => { | |
console.error( err ); | |
process.exit( 2 ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment