-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (59 loc) · 1.71 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
65
66
67
68
69
70
const express = require('express');
const axios = require('axios');
const cheerio = require("cheerio")
const app = express();
app.use(express.json());
async function getSiteData(){
await axios("https://www.octaveclothing.com/men").then((response) => {
const html_data = response.data;
console.log(html_data);
const $ = cheerio.load(html_data);
})
}
getSiteData();
async function cryptopriceScraper() {
const url = "https://www.octaveclothing.com/men";
const result = [];
await axios(url).then((response) => {
const html_data = response.data;
const $ = cheerio.load(html_data);
const keys = ["Title","Description","Price"];
const selectedElem = ".views-infinite-scroll-content-wrapper > .row > .col-6 > .product-7 > .product-body"
$(selectedElem).each((parentIndex, parentElem) => {
let keyIndex = 0;
const data = {};
if (parentIndex) {
$(parentElem)
.children()
.each((childId, childElem) => {
const value = $(childElem).text();
if (value) {
data[keys[keyIndex]] = value;
keyIndex++;
}
});
result.push(data);
}
});
});
return result;
}
app.get("/data-scrapper", async (req, res) => {
try {
const data = await cryptopriceScraper();
return res.status(200).json({
result: data,
});
await getSiteData();
} catch (err) {
return res.status(500).json({
err: err.toString(),
});
}
});
app.get('/', (req, res) => {
res.status(200).send("Welcome");
})
app.listen(5000, () => {
console.log("Server started at port 5000");
})