-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.js
53 lines (43 loc) · 1.41 KB
/
scraper.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const path = require('path');
const fs = require('fs');
const config = require('./config');
const { SELECTORS, SITE_ROOT_URL } = config;
const { card } = SELECTORS;
const ELEMS = {
CARD: card.selector,
CARD_TITLE: card.selector + ' ' + card.children.title,
AUTHOR: card.selector + ' ' + card.children.author,
COMPANY: card.selector + ' ' + card.children.company,
LENGTH: card.selector + ' ' + card.children.length,
};
const scrapData = async (page) => {
const courseTitles = await page.$$eval(ELEMS.CARD_TITLE, (elems) =>
elems.map((el) => ({
url: el.getAttribute('href'),
title: el.innerText,
}))
);
const authors = await page.$$eval(ELEMS.AUTHOR, (elems) =>
elems.map((el) => el.innerText)
);
const companies = await page.$$eval(ELEMS.COMPANY, (elems) =>
elems.map((el) => el.innerText)
);
const courseLengths = await page.$$eval(ELEMS.LENGTH, (elems) =>
elems.map((el) => el.innerText)
);
const data = courseTitles.map((titleObj, idx) => ({
title: titleObj.title,
url: SITE_ROOT_URL + titleObj.url.slice(0, -1),
author: authors[idx],
authorFrom: companies[idx],
length: courseLengths[idx]?.replace(' CC', '')
}));
fs.writeFile(path.join(__dirname, 'courses.json'), JSON.stringify(data), (err) => {
if (err) return console.log('err', err);
console.log('successfully written to courses.json');
})
};
module.exports = {
scrapData,
};