-
Notifications
You must be signed in to change notification settings - Fork 0
/
productScraper.js
88 lines (72 loc) · 2.41 KB
/
productScraper.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const puppeteer = require("puppeteer");
const dotenv = require('dotenv');
dotenv.config();
const productScraper = async (url) => {
const browser = await puppeteer.connect({ browserWSEndpoint: `wss://chrome.browserless.io?token=${process.env.TOKEN}` })
const page = await browser.newPage();
await page.goto(url, { timeout: 60000 });
await page.waitForSelector(".product__price");
await page.waitForSelector(".product__name");
await page.waitForSelector(".product__region");
await page.waitFor(3000);
// extracting information from code
let productDetails = await page.evaluate(() => {
let percentage = "";
let price = "";
let name = "";
let country = "";
let region = "";
const contentItem = document.body.querySelector(".product__properties-list__content-item");
if (contentItem) {
const textContent = contentItem.children[1].textContent;
if (textContent.includes('%')) {
percentage = textContent;
}
}
document.body.querySelector(".product__price")
? (price = document.body.querySelector(".product__price").textContent)
: (price = "");
document.body.querySelector(".product__name")
? (name = document.body.querySelector(".product__name").textContent)
: (percentage = "");
document.body.querySelector(".product__region").children[0]
? (country =
document.body.querySelector(".product__region").children[0]
.textContent)
: (country = "");
document.body.querySelector(".product__region").children[1]
? (region =
document.body.querySelector(".product__region").children[1]
.textContent)
: (region = "Øvrige");
let productDetailsElement = document.body.querySelectorAll(
".product__tab-list > li"
);
let productDetails = Object.values(productDetailsElement).map((x) => {
let type = x.getElementsByTagName("span")[0].textContent ?? null;
let value = x.getElementsByTagName("span")[1].textContent ?? null;
return {
type,
value,
};
});
let data = {};
data = {
country: country,
region: region,
name: name,
price: price,
percentage: percentage,
productID: "",
destilery: "",
colour: "",
taste: "",
odour: "",
details: productDetails,
};
return data;
});
await browser.close();
return productDetails;
};
module.exports = productScraper;