-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
179 lines (144 loc) · 7.28 KB
/
index.js
File metadata and controls
179 lines (144 loc) · 7.28 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const chalk = require('chalk');
const readline = require('readline');
puppeteer.use(StealthPlugin());
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const waitForUser = async () => {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise(resolve => {
rl.question(chalk.yellow('\n[WAITING...] Your interaction is required to proceed... '), (ans) => {
rl.close();
resolve(ans);
});
});
};
async function scrapeCeneo(productId, targetCount, page) {
let collectedData = [];
let currentPage = 1;
let productNameSafe = "Unknown_Product";
console.log(chalk.cyan(`\n[Ceneo] Starting for ID: ${productId}`));
while (collectedData.length < targetCount) {
await page.goto(`https://www.ceneo.pl/${productId}/opinie-${currentPage}`, { waitUntil: 'domcontentloaded' });
if (currentPage === 1) {
const title = await page.evaluate(() => document.querySelector('h1.product-top__product-info__name')?.innerText.trim() || 'Product');
productNameSafe = title.replace(/[^a-zA-Z0-9\s]/g, "").replace(/\s+/g, "_").substring(0, 40);
}
const hasReviews = await page.$('.js_product-review');
if (!hasReviews) break;
const reviews = await page.evaluate((pName) => {
return Array.from(document.querySelectorAll('.js_product-review')).map(card => {
const stars = parseFloat(card.querySelector('.user-post__score-count')?.innerText.split('/')[0].replace(',', '.') || 0);
return {
product: pName,
score: (stars / 5).toFixed(3),
sentiment: stars >= 2.5 ? "P" : "N",
content: card.querySelector('.user-post__text')?.innerText.trim()
};
});
}, productNameSafe);
collectedData = [...collectedData, ...reviews];
console.log(chalk.gray(`Strona ${currentPage} | Pobrano: ${collectedData.length}/${targetCount}`));
if (reviews.length === 0) break;
currentPage++;
await sleep(1500);
}
return collectedData.slice(0, targetCount);
}
async function scrapeAmazon(productId, targetCount, page) {
let collectedData = [];
const url = `https://www.amazon.pl/product-reviews/${productId}/ref=cm_cr_dp_d_show_all_btm?reviewerType=all_reviews`;
console.log(chalk.cyan(`\n[Amazon.pl] Starting for ID: ${productId}`));
await page.setExtraHTTPHeaders({ 'Accept-Language': 'pl-PL,pl;q=0.9' });
await page.goto(url, { waitUntil: 'domcontentloaded' });
await waitForUser();
while (collectedData.length < targetCount) {
await page.waitForSelector('[data-hook="review"]', { timeout: 5000 }).catch(() => {});
const data = await page.evaluate(() => {
const reviews = Array.from(document.querySelectorAll('[data-hook="review"]'));
const pLink = document.querySelector('a[data-hook="product-link"]');
const pName = pLink ? pLink.innerText.trim().substring(0, 50) : "Amazon_Product";
return reviews.map(card => {
const starsText = card.querySelector('[data-hook="review-star-rating"] .a-icon-alt')?.innerText || "0";
const stars = parseFloat(starsText.replace(',', '.').split(' ')[0]);
return {
product: pName,
score: (stars / 5).toFixed(3),
sentiment: stars >= 3.0 ? "P" : "N",
content: card.querySelector('[data-hook="review-body"] span')?.innerText.trim()
};
});
});
collectedData = [...collectedData, ...data];
console.log(chalk.gray(`Pobrano łącznie: ${collectedData.length}/${targetCount}`));
if (collectedData.length >= targetCount) break;
const nextBtn = await page.$('li.a-last a');
if (!nextBtn) {
console.log(chalk.yellow("Page ended or no next button found."));
break;
}
try {
await Promise.all([
page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 }),
nextBtn.click()
]);
await sleep(2000 + Math.random() * 2000);
} catch (e) {
console.log(chalk.red("Error navigating to next page."));
break;
}
}
return collectedData.slice(0, targetCount);
}
async function main() {
console.clear();
console.log(chalk.blue.bold('======================================='));
console.log(chalk.yellow.bold(' REVIEWS SCRAPPER: CENEO & AMAZON '));
console.log(chalk.blue.bold('=======================================\n'));
const settings = await inquirer.prompt([
{ type: 'list', name: 'engine', message: 'Select service:', choices: ['Ceneo', 'Amazon'] },
{ type: 'input', name: 'id', message: 'Enter Produkt ID (e.g. B0D3658SHD or 163090037):' },
{ type: 'number', name: 'limit', message: 'How many reviews to collect?', default: 50 },
{ type: 'list', name: 'format', message: 'Output format', choices: ['Single JSON file', 'Multiple TXT files'] }
]);
const isHeadless = settings.engine === 'Ceneo' ? "new" : false;
const browser = await puppeteer.launch({ headless: isHeadless, args: ['--start-maximized'] });
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
let results = [];
if (settings.engine === 'Ceneo') {
results = await scrapeCeneo(settings.id, settings.limit, page);
} else {
results = await scrapeAmazon(settings.id, settings.limit, page);
}
const outputDir = path.resolve(__dirname, 'reviews', settings.engine.toLowerCase());
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
if (settings.format === 'Single JSON file') {
const filePath = path.join(outputDir, `data_${settings.id}.json`);
const finalExport = {
productId: settings.id,
productName: results.length > 0 ? results[0].product : "Unknown",
engine: settings.engine,
scrapedAt: new Date().toISOString(),
totalReviews: results.length,
reviews: results.map(({ score, sentiment, content }) => ({
score,
sentiment,
content
}))
};
fs.writeFileSync(filePath, JSON.stringify(finalExport, null, 2));
console.log(chalk.green(`\nSuccess! JSON file created for product: ${chalk.white.bold(filePath)}`));
} else {
results.forEach((r, i) => {
const fileName = `${r.product.replace(/\s/g, '_')}_${r.score}_${r.sentiment}_${i+1}.txt`;
fs.writeFileSync(path.join(outputDir, fileName), r.content);
});
console.log(chalk.green(`\nSuccess! Created ${results.length} TXT files. Check the folder: ${chalk.white.bold(outputDir)}`));
}
await browser.close();
}
main();