feat: add Playwright-based fallback scraper
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013gwc3aCiyQxmCWQke8RvEz
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
const { chromium } = require('playwright');
|
||||
const { extractPrice } = require('./scraper');
|
||||
|
||||
async function launchBrowser() {
|
||||
return chromium.launch({ headless: true });
|
||||
}
|
||||
|
||||
async function fetchAndExtractPriceViaBrowser(browser, url, { timeoutMs = 20000 } = {}) {
|
||||
let page;
|
||||
try {
|
||||
page = await browser.newPage();
|
||||
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: timeoutMs });
|
||||
const html = await page.content();
|
||||
const { price, method } = extractPrice(html);
|
||||
return { price, method, error: null };
|
||||
} catch (err) {
|
||||
return { price: null, method: null, error: err };
|
||||
} finally {
|
||||
if (page) {
|
||||
await page.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { launchBrowser, fetchAndExtractPriceViaBrowser };
|
||||
Reference in New Issue
Block a user