-
Notifications
You must be signed in to change notification settings - Fork 1
/
spider.js
53 lines (40 loc) · 1.4 KB
/
spider.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
import * as cheerio from 'cheerio';
import fs from 'fs';
let contentLinkArray = [];
const domain = 'https://fek.io/';
const initialPage = 'blog';
const fisrtPage = `${domain}/${initialPage}`;
let pagetext = await pageLoader(fisrtPage);
await recursivePageLoader(pagetext);
const writeStream = fs.createWriteStream('output.txt');
await readAllURLsToFile(contentLinkArray);
async function readAllURLsToFile(links) {
for (const url of links) {
let blogPostURL = `${domain}${url}`;
let posttext = await pageLoader(blogPostURL);
let $ = cheerio.load(posttext);
let blogText = $('article').text();
writeStream.write(blogText + '\n\n\n\n');
}
writeStream.end();
}
async function pageLoader(url) {
const pagedata = await fetch(url);
const pagetext = await pagedata.text();
return pagetext;
}
async function recursivePageLoader(pagetext) {
const aClassSelector = 'div.bloglistitem-module--listitem--99998 > div > a';
const $ = cheerio.load(pagetext);
const links = $(aClassSelector);
for (let linkObj of links) {
contentLinkArray.push(linkObj.attribs.href);
}
let nextLink = $('a[rel="next"]');
if (nextLink[0]) {
console.log(nextLink[0].attribs.href);
const nextPage = `${domain}/${nextLink[0].attribs.href}`;
const nextPageText = await pageLoader(nextPage);
await recursivePageLoader(nextPageText);
}
}