Skip to content

Instantly share code, notes, and snippets.

@pkpp1233
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save pkpp1233/05989efaa76ab3568bc2 to your computer and use it in GitHub Desktop.

Select an option

Save pkpp1233/05989efaa76ab3568bc2 to your computer and use it in GitHub Desktop.
Pull ESPN Headers with UI

Clone and run npm install.

Go to homepage, type in a sport, submit, and receive the latest headlines.

Sports:
"" = all
"nhl" = NHL
"mlb" = MLB
"nba" = NBA
"college-football" = College football
"mens-college-basketball" = Mens college basketball
"racing/nascar" = NASCAR

<html>
<body>
<form accept-charset="UTF-8" action="/scrape" method="post">
<input autofocus="autofocus" placeholder="nba" type="text" name="sport">
<input type="submit" value="Get my headlines!">
<br>
</form>
</body>
</html>
{
"name" : "espn-scrape",
"version" : "0.0.1",
"description" : "Scrape espn headlines.",
"main" : "server.js",
"author" : "Blockspring",
"dependencies" : {
"express" : "latest",
"request" : "latest",
"cheerio" : "latest",
"ejs" : "latest",
"body-parser": "latest"
}
}
var express = require('express');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
var bodyParser = require('body-parser');
app.set('views', __dirname);
app.set("view engine", "ejs");
app.use(bodyParser());
app.get('/', function(req, res){
res.render("index");
});
app.post('/scrape', function(req, res){
var sport = req.body.sport;
request('http://www.espn.com/' + sport, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var headlines = []
$('.headlines li').each(function(i, elem){
headlines.push($(this).text());
})
res.send(headlines);
} else {
console.log("Could not find espn headlines.");
}
});
});
app.listen('8081');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment