41 lines
1.4 KiB
JavaScript
41 lines
1.4 KiB
JavaScript
// khanoumi.js
|
|
const puppeteer = require("puppeteer");
|
|
|
|
async function searchKhanoumi(query, limit = 12) {
|
|
if (!query) return [];
|
|
|
|
let browser;
|
|
try {
|
|
browser = await puppeteer.launch({
|
|
headless: true,
|
|
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
|
});
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(`https://khanoumi.com/search?q=${encodeURIComponent(query)}`, {
|
|
waitUntil: 'networkidle2',
|
|
timeout: 30000
|
|
});
|
|
|
|
const products = await page.evaluate((limit) => {
|
|
const items = Array.from(document.querySelectorAll('[data-product-id]'));
|
|
return items.slice(0, limit).map(item => {
|
|
const title = item.querySelector('[data-product-title]')?.textContent || "بدون عنوان";
|
|
const price = item.querySelector('[data-product-price]')?.textContent || "—";
|
|
const image = item.querySelector('img')?.src || "https://via.placeholder.com/150";
|
|
const link = item.querySelector('a')?.href || "#";
|
|
|
|
return { title, price, image, link };
|
|
});
|
|
}, limit);
|
|
|
|
return products;
|
|
} catch (err) {
|
|
console.error("Khanoumi scraping error:", err.message);
|
|
return [];
|
|
} finally {
|
|
if (browser) await browser.close();
|
|
}
|
|
}
|
|
|
|
module.exports = { searchKhanoumi }; |