News Feed Forums General Web Scraping How to extract classified ads from LeBonCoin.fr using JavaScript?

  • How to extract classified ads from LeBonCoin.fr using JavaScript?

    Posted by Kliment Pandu on 12/21/2024 at 7:49 am

    Scraping classified ads from LeBonCoin.fr using JavaScript allows you to collect data on items for sale, rental properties, and services. LeBonCoin is one of France’s largest classified ad platforms, making it a valuable source for market research. Using Node.js with Puppeteer, you can automate browser interactions to handle dynamic content and extract relevant details such as ad titles, prices, and locations. The process begins with inspecting the website structure to identify the HTML elements containing the desired data points.
    Pagination is critical for accessing all ads across multiple pages, ensuring a complete dataset. Automating navigation and introducing delays between requests mimics human browsing behavior and reduces detection risks. Once collected, the data can be stored in structured formats for analysis. Below is an example Node.js script for scraping LeBonCoin ads.

    const puppeteer = require('puppeteer');
    (async () => {
        const browser = await puppeteer.launch({ headless: true });
        const page = await browser.newPage();
        const url = 'https://www.leboncoin.fr/';
        await page.goto(url, { waitUntil: 'networkidle2' });
        const ads = await page.evaluate(() => {
            const adList = [];
            const items = document.querySelectorAll('.ad-card');
            items.forEach(item => {
                const title = item.querySelector('.ad-title')?.textContent.trim() || 'Title not available';
                const price = item.querySelector('.ad-price')?.textContent.trim() || 'Price not available';
                const location = item.querySelector('.ad-location')?.textContent.trim() || 'Location not available';
                adList.push({ title, price, location });
            });
            return adList;
        });
        console.log(ads);
        await browser.close();
    })();
    

    This script collects ad titles, prices, and locations from LeBonCoin.fr’s classified sections. Pagination handling allows the scraper to navigate through multiple pages and capture all available ads. Random delays between requests help avoid detection, ensuring smooth operation.

    Kliment Pandu replied 1 day, 10 hours ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

Log in to reply.