57 lines
2.1 KiB
JavaScript
57 lines
2.1 KiB
JavaScript
import puppeteer from 'puppeteer';
|
|
import Listing from '../../server/db/models/Listing';
|
|
//import { notifyNewListing } from './telegramBot';
|
|
|
|
export default async function scrapeFotocasa() {
|
|
console.log('Starting Fotocasa scraping process');
|
|
|
|
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox'] });
|
|
const page = await browser.newPage();
|
|
await page.goto('https://www.fotocasa.es/es/comprar/viviendas/granada-capital/todas-las-zonas/l');
|
|
const pageSourceHTML = await page.content();
|
|
await browser.close();
|
|
console.log('Navigated to Fotocasa',pageSourceHTML);
|
|
|
|
try {
|
|
|
|
const listings = await page.evaluate(() => {
|
|
const results = [];
|
|
document.querySelectorAll('.re-CardPackPremium').forEach((element) => {
|
|
const title = element.querySelector('.re-CardTitle')?.innerText.trim();
|
|
const priceText = element.querySelector('.re-CardPrice')?.innerText.trim();
|
|
const price = parseFloat(priceText.replace(/[^\d]/g, ''));
|
|
const location = title;
|
|
const description = element.querySelector('.re-CardDescription-text')?.innerText.trim();
|
|
const url = element.querySelector('a')?.href;
|
|
|
|
if (title && price && location && description && url) {
|
|
results.push({ title, price, location, description, url });
|
|
}
|
|
console.log(title, price, location, description, url);
|
|
});
|
|
return results;
|
|
});
|
|
|
|
console.log(`Found ${listings.length} listings`);
|
|
|
|
for (const listingData of listings) {
|
|
try {
|
|
const listing = new Listing(listingData);
|
|
//await listing.save();
|
|
console.log(`Saved listing to database: ${listing.title}`);
|
|
|
|
//await notifyNewListing(listing);
|
|
console.log(`Sent notification for listing: ${listing.title}`);
|
|
} catch (innerError) {
|
|
console.error(`Error processing listing: ${innerError.message}`);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error scraping Fotocasa:', error.message);
|
|
throw new Error('Failed to scrape Fotocasa');
|
|
} finally {
|
|
await browser.close();
|
|
console.log('Finished Fotocasa scraping process');
|
|
}
|
|
}
|