-
Notifications
You must be signed in to change notification settings - Fork 1
/
webscrapper.js
50 lines (48 loc) · 1.64 KB
/
webscrapper.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
const axios = require("axios");
const cheerio = require("cheerio");
require("dotenv").config();
url = process.env.URL
async function scrapeData() {
try {
const unsortedArray = [];
const { data } = await axios.get(url);
const $ = cheerio.load(data);
const list = $(
"#post-253 > div > figure:nth-child(21) > table > tbody > tr > td"
);
//console.log(list);
list.each((idx, el) => {
unsortedArray.push($(el).text());
});
const objectKey = unsortedArray.filter(function (value, index, arr) {
return index < 4;
});
objectKey[2] = "Selling";
objectKey[3] = "Buying";
for (let index = 0; index < 4; index++) {
unsortedArray.shift();
}
groupedArray = [];
while (unsortedArray.length) {
groupedArray.push(unsortedArray.splice(0, 4));
}
const currencyConversion = [];
for (let index = 0; index < groupedArray.length; index++) {
const objects = {};
objectKey.forEach((key, i) => (objects[key] = groupedArray[index][i]));
currencyConversion.push(objects);
}
var arrayAsString = [];
for (let index = 0; index < currencyConversion.length; index++) {
const objectAsString = `${objectKey[1]}: ${currencyConversion[index].Currency} \n ${objectKey[2]}: ${currencyConversion[index].Selling} \n ${objectKey[3]}: ${currencyConversion[index].Buying} \n`;
arrayAsString.push(objectAsString);
}
arrayAsString = [`${objectKey[0]}: ${currencyConversion[0].Date} `, ...arrayAsString]
const answer = arrayAsString.join("\n");
return answer;
} catch (error) {
console.log(error);
}
}
scrapeData();
module.exports = { scrapeData };