-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
34 lines (26 loc) · 792 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
var url = "https://www.reddit.com/hot/";
request(url, function (err, res, html){
if(!err) {
var $ = cheerio.load(html);
var allItems = $('#siteTable').children();
var items = [];
allItems.each(function (index){
var result = $('#siteTable').children().eq(index).children().eq(4).find('a.title').text();
if(result !== "") { //if result is not empty string the push to array
items.push(result);
}
});
//write data
fs.writeFile('output.txt', JSON.stringify(items, null, 4), function(err){
if(err) {
console.log(err);
} else {
console.log(`Scraped data from ${url} has been added to a file.`);
}
})
console.log(items);
}
});