Example Node (4) script that pulls posts from the front page of Hacker News
/** | |
* 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