-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
91 lines (72 loc) · 3.02 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import puppeteer from "puppeteer";
import * as fs from 'fs';
import process from 'process';
const exporToCSV = (data, filename) => {
const csvContent = `Name,Rating,Location,Shop,Sold,Discount,Image\n${data.map(item => Object.values(item).join(',')).join('\n')}`;
fs.writeFileSync(filename, csvContent);
};
async function getProducts(keyword, filename) {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
});
const page = await browser.newPage();
await page.goto(`https://www.tokopedia.com/search?q=${keyword}`, {
waitUntil: "domcontentloaded",
});
async function autoScroll(page) {
await page.evaluate(async () => {
await new Promise((resolve) => {
let totalHeight = 0;
const distance = 100;
const timer = setInterval(() => {
const scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 100);
});
});
}
await autoScroll(page);
const products = await page.evaluate(() => {
const result = [];
const productElements = document.querySelectorAll('.prd_link-product-name.css-3um8ox');
const productNames = Array.from(productElements).map(name =>
name.innerText.trim()
);
const ratingElements = document.querySelectorAll('div.prd_shop-rating-average-and-label > span.prd_rating-average-text');
const productRatings = Array.from(ratingElements).map(rating => rating.innerText.trim());
const locationElements = document.querySelectorAll('div.css-1rn0irl > span.prd_link-shop-loc');
const productLocations = Array.from(locationElements).map(location => location.innerText.trim());
const shopElements = document.querySelectorAll('span.prd_link-shop-name');
const productShops = Array.from(shopElements).map(shop => shop.innerText.trim());
const soldElements = document.querySelectorAll('span.prd_label-integrity');
const productSolds = Array.from(soldElements).map(sold => sold.innerText.trim());
const discountElements = document.querySelectorAll('.prd_badge-product-discount');
const productDiscounts = Array.from(discountElements).map(discount => discount.innerText.trim());
const images = Array.from(document.querySelectorAll('.pcv3_img_container > img'));
const productImages = images.map(img => img.src)
for (let i = 0; i < Math.min(productNames.length); i++) {
const discount = productDiscounts[i] !== undefined ? productDiscounts[i] : 0;
result.push({
name: productNames[i],
rating: productRatings[i],
location: productLocations[i],
shop: productShops[i],
sold: productSolds[i],
discount,
image: productImages[i]
});
}
return result;
});
exporToCSV(products, `${filename}.csv`)
await browser.close();
};
const keyword = process.argv[2]
const filename = process.argv[3]
getProducts(keyword, filename);