-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (43 loc) · 1.56 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
import cheerio from "cheerio";
import saver from "./handlers/saver.js";
import { arrayFromLength, clean } from "./helpers/common.js";
import { getPageContent } from "./helpers/puppeteer.js";
const SITE =
"https://www.okeydostavka.ru/spb/bakaleia-i-konservatsiia/konservatsiia/ovoshchnaia-i-gribnaia-konservatsiia#face&productBeginIndex:";
const totalPages = 4;
(async function main() {
try {
const productItems = [];
for (const productBeginIndex of arrayFromLength(totalPages)) {
const url = `${SITE}${productBeginIndex}`;
const pageContent = await getPageContent(url);
const $ = cheerio.load(pageContent);
$(".product").each((i, card) => {
let discountPrice = null;
let price = null;
const title = $(card).find(".product-name").find("a").text();
const weight = $(card).find(".product-weight").text();
const discount = $(card).find(".special-discount").find("span").text();
if (discount !== "") {
discountPrice = $(card).find(".product-price").find("input").val();
price = $(card).find(".product-price").find(".crossed").text();
price = price.substring(0, price.indexOf("₽")) + "₽";
} else {
price = $(card).find(".product-price").find("input").val();
}
let item = {
position: i + 1,
title,
weight,
price,
discountPrice,
};
clean(item);
productItems.push(item);
saver(productItems);
});
}
} catch (err) {
console.log(err);
}
})();