bid-tool/scrape-data-keyword/models/grays-scrap-model.js

73 lines
1.9 KiB
JavaScript

import { extractModelId, extractNumber } from "../system/ultils.js";
import { ScrapModel } from "./scrap-model.js";
export class GraysScrapModel extends ScrapModel {
action = async () => {
const urlsData = this.extractUrls();
for (let item of urlsData) {
await this.page.goto(item.url);
const data = await this.getItemsInHtml(item);
const results = this.filterItemByKeyword(item.keyword, data);
this.results[item.keyword] = results;
}
console.log({ results: this.results });
};
getPriceByEl = async (elementHandle) => {
const selectors = [
"a > div:nth-child(2) > div:nth-child(2)", // Single product price
"a > div:nth-of-type(2) > div:nth-of-type(2) > div > div:nth-of-type(1) > div", // Multiple product price
];
for (const selector of selectors) {
const priceText = await elementHandle
.$eval(selector, (el) => el.textContent.trim())
.catch(() => null);
if (priceText) {
const price = extractNumber(priceText);
if (price) return price;
}
}
return null;
};
getItemsInHtml = async (data) => {
const elements = await this.page.$$(".sc-102aeaf3-1.eYPitT > div");
const results = [];
for (const el of elements) {
const url = await el
.$eval("a", (el) => el.getAttribute("href"))
.catch(() => null);
const image_url = await el
.$eval("img", (img) => img.getAttribute("src"))
.catch(() => null);
const name = await el
.$eval("h2", (el) => el.textContent.trim())
.catch(() => null);
const current_price = await this.getPriceByEl(el); // Gọi hàm async được định nghĩa trong class
results.push({
url,
image_url,
name,
keyword: data.keyword,
model: extractModelId(url),
current_price,
scrap_config_id: this.scrap_config_id,
});
}
return results;
};
}