// Following code gets celeb names from IMDB who born on current date, search their brief information in wiki and finally print in a form of table.
Prerequisite NPM commands:
npm init -y
npm install request --save
npm install request-promise --save
npm install cheerio--save
npm install cli-table--save
Code starts from here
const rp = require('request-promise');
const cheerio = require('cheerio');
const Table = require('cli-table');
let table = new Table({
head: ['userName', ' userInfo'],
colWidths: [15, 200,]
})
const options = {
url: `http://www.imdb.com/`,
json: true,
transform: body => cheerio.load(body)
}
rp(options)
.then(function ($) {
let userData = [];
// Get the div which has class 'widget_caption caption_below' residing under div with class 'ab_borntoday'
var bornToday = $('div.ab_borntoday').find('.widget_caption.caption_below');
var name;
if(bornToday.length>0){
bornToday.each(function(i, elem) {
name = $(this).text().trim().replace(' ','_').split('(')[0];
console.log("Name:"+name)
userData.push({name:name});
});
}
getUserDetails(userData);
})
.catch((err) => {
console.log(err);
});
function getUserDetails(userData) {
var i = 0;
function next() {
if (i < userData.length) {
var options = {
url: `https://en.wikipedia.org/wiki/` + userData[i].name,
transform: body => cheerio.load(body)
}
rp(options)
.then(function ($) {
process.stdout.write(`.`);
var userInfo = $('div.mw-parser-output').find('p').text().substring(0,150);
table.push([userData[i].name, userInfo]);
++i;
return next();
})
}else {
printData();
}
}
return next();
};
function printData() {
console.log("✅");
console.log(table.toString());
}
Prerequisite NPM commands:
npm init -y
npm install request --save
npm install request-promise --save
npm install cheerio--save
npm install cli-table--save
Code starts from here
const rp = require('request-promise');
const cheerio = require('cheerio');
const Table = require('cli-table');
let table = new Table({
head: ['userName', ' userInfo'],
colWidths: [15, 200,]
})
const options = {
url: `http://www.imdb.com/`,
json: true,
transform: body => cheerio.load(body)
}
rp(options)
.then(function ($) {
let userData = [];
// Get the div which has class 'widget_caption caption_below' residing under div with class 'ab_borntoday'
var bornToday = $('div.ab_borntoday').find('.widget_caption.caption_below');
var name;
if(bornToday.length>0){
bornToday.each(function(i, elem) {
name = $(this).text().trim().replace(' ','_').split('(')[0];
console.log("Name:"+name)
userData.push({name:name});
});
}
getUserDetails(userData);
})
.catch((err) => {
console.log(err);
});
function getUserDetails(userData) {
var i = 0;
function next() {
if (i < userData.length) {
var options = {
url: `https://en.wikipedia.org/wiki/` + userData[i].name,
transform: body => cheerio.load(body)
}
rp(options)
.then(function ($) {
process.stdout.write(`.`);
var userInfo = $('div.mw-parser-output').find('p').text().substring(0,150);
table.push([userData[i].name, userInfo]);
++i;
return next();
})
}else {
printData();
}
}
return next();
};
function printData() {
console.log("✅");
console.log(table.toString());
}
No comments:
Post a Comment