-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrabber.js
41 lines (34 loc) · 1.1 KB
/
grabber.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
34
35
36
37
38
39
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs-extra');
const grab = (callback) => {
let data = [];
let tab = 0;
const timer = setInterval(async () => {
console.log('current tab:', tab);
const html = await axios.get(`http://topviccek.hu?page=${tab}`);
const $ = await cheerio.load(html.data);
const content = $('.tabContent');
if (!content || content.length <= 0) {
callback(data);
clearInterval(timer);
return
} else {
$('.tabContent').each((i, elem) => {
const typ = $(elem).find('.cat').find('a').text();
const title = $(elem).find('.title').text().trim();
const text = $(elem).find('.joke').text().trim();
data.push({
typ,
title,
text
});
})
tab++;
}
}, 1000)
}
grab((data) => {
fs.writeJSON('jokes.json', data, { spaces: '\t' });
console.log('saved to: jokes.json');
})