forked from jayhung97724/106-2NCHUIT-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchIndex.js
29 lines (26 loc) · 1.01 KB
/
fetchIndex.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
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
let HotBoardsJson = [];
let url = 'https://www.ptt.cc'
request(url + '/bbs/index.html', (error, response, body) => {
console.log('error: ', error);
console.log('sratusCode: ', response && response.statusCode);
// console.log('body: ', body);
var $ = cheerio.load(body);
boardArr = $('a.board');
for (var i = 0; i < boardArr.length; i++) {
boardName = $($(boardArr[i]).find('.board-name')).text();
boardClass = $($(boardArr[i]).find('.board-class')).text();
boardLink = url + $(boardArr[i]).attr('href');
HotBoardsJson.push({ boardName: boardName, boardClass: boardClass, boardLink: boardLink })
console.log(`${boardName}\n>> ${boardLink}\n>> 分類:${boardClass}\n`);
}
fs.writeFile('HotBoards.json', JSON.stringify(HotBoardsJson), function (err) {
if (err)
console.log(err);
else
console.log('File ' + 'HotBoards.json' + ' written!');
})
console.log('共 ' + HotBoardsJson.length + ' 篇\n');
});