Last active
July 31, 2017 16:12
-
-
Save CarlMungazi/98669798ed427d083bc2 to your computer and use it in GitHub Desktop.
Scraping and reversing headlines with node.js
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
//require modules | |
var request = require('request'); | |
var cheerio = require('cheerio'); | |
var fs = require('fs'); | |
//prepare to reverse those strings | |
function reverseString(str) { | |
return str | |
.split('') //split string into an array | |
.reverse() // reverse the array values | |
.join('') // join the array values | |
}; | |
//grab the url | |
request("http://www.luton-dunstable.co.uk/news", | |
function(error, response, body) { | |
//spit out error message if things go south | |
if(error) { | |
console.log("Error :" + error); | |
} | |
//log the status code | |
console.log("Status code: " + response.statusCode); | |
//load the web page | |
var $ = cheerio.load(body); | |
//start grabbing data under the div#body selector | |
$('div#body').each(function( index ){ | |
var pageTitle = $(this).find('h1.section__heading').text().trim(); | |
var storyTitle = $(this).find('h3.heading.heading--l').text().trim(); | |
//log the results | |
console.log(pageTitle); | |
console.log("Headlines " + reverseString(storyTitle)); | |
//create .txt file with reversed headlines | |
fs.appendFileSync('reverse-headlines.txt', pageTitle + '\n' + reverseString(storyTitle) + '\n'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment