Intro To Web Scraping With Node.js & Cheerio – Download

Affiliate Program Get Money from your Website



In this video we will take a look at the Node.js library, Cheerio which is a jQuery like tool for the server used in web scraping. This is similar to the python video that I did on web scraping

Sponsor:
DevMountain Bootcamp – https://goo.gl/6q0dEa

Code For This Project:

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…');
}
});

Become a Patron: Show support & get perks!
http://www.patreon.com/traversymedia

Website & Udemy Courses
http://www.traversymedia.com

Follow Traversy Media:
https://www.facebook.com/traversymedia


Source