Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iskandarsaleh/80d986cf754fff76fd58af3ccf4f50f8 to your computer and use it in GitHub Desktop.
Save iskandarsaleh/80d986cf754fff76fd58af3ccf4f50f8 to your computer and use it in GitHub Desktop.
Simple example to scrape some posts and put into a CSV file using Node & Cheerio
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const writeStream = fs.createWriteStream('post.csv');
// Write Headers
writeStream.write(`Title,Link,Date \n`);
request('http://codedemos.com/sampleblog', (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
$('.post-preview').each((i, el) => {
const title = $(el)
.find('.post-title')
.text()
.replace(/\s\s+/g, '');
const link = $(el)
.find('a')
.attr('href');
const date = $(el)
.find('.post-date')
.text()
.replace(/,/, '');
// Write Row To CSV
writeStream.write(`${title}, ${link}, ${date} \n`);
});
console.log('Scraping Done...');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment