-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (50 loc) · 1.55 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
const axios = require("axios");
const cheerio = require("cheerio");
const fs = require("fs");
const json2csv = require("json2csv").Parser;
const baseUrl = "https://stackoverflow.com/questions";
const newUrl = (page) => {
return `https://stackoverflow.com/questions?tab=newest&page=${page}`;
};
const crawlPage = async () => {
try {
let records = [];
let page = 1;
let firstResponse = await axios.get(newUrl(page));
let $ = cheerio.load(firstResponse.data);
const lastPage = $(
"#mainbar > div.s-pagination.site1.themed.pager.float-left > a:nth-child(7)"
).text();
while (page <= lastPage) {
let response = await axios.get(newUrl(page));
let $ = cheerio.load(response.data);
let a = $('div[id="questions"] > div ');
for (var i = 0; i < a.length; i++) {
let id = a[i].attribs.id;
let views = $(`#${id} > div.statscontainer > div.views`)
.text()
.match(/\d/g)
.join("");
let votes = $(
`#${id} > div.statscontainer > div.stats > div.vote > div > span > strong`
).text();
let answers = $(
`#${id} > div.statscontainer > div.stats > div.status.unanswered > strong`
).text();
records.push({
URL: newUrl(page),
views,
votes,
answers,
});
}
const j2c = new json2csv();
const csv = j2c.parse(records);
fs.writeFileSync("./Data/data.csv", csv, "utf-8");
page = page + 1;
}
} catch (err) {
console.log(err);
}
};
crawlPage();