Actor picture

Leboncoin extractor

anchor/leboncoin

Extract information from leboncoin.fr : simply provide your search URL (+ your filters and location) and the number of times you want to click on "Page Suivante".

Free trial for 30 days

Then $9/month

No credit card required now

Author's avatarguillim
  • Modified
  • Users23
  • Runs387
Actor picture
Leboncoin extractor

Free trial for 30 days

Then $9/month

To run the code examples, you need to have an Apify account. Replace <YOUR_API_TOKEN> in the code with your API token. For a more detailed explanation, please read about running actors via the API in Apify Docs.

const { ApifyClient } = require('apify-client');

// Initialize the ApifyClient with API token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare actor input
const input = {
    "startUrls": [
        {
            "url": "https://www.leboncoin.fr/recherche?category=21&text=mer&price=17-50"
        }
    ],
    "pageFunction": async function pageFunction(context) {
        let data = {}
        let userData = context.request.userData
        data.url = context.request.url
        data.label = userData.label
        // data.title = await context.page.title();
        // context.log.info(data.title);
    
        if(userData && userData.label === 'product'){   
            context.log.info('label product.');     
            data.img = await context.page.locator('[data-qa-id=adview_spotlight_container] img >> nth=0').getAttribute('src')
            data.title = await context.page.locator('[data-qa-id=adview_title] >> nth=0').innerText()
            data.price = await context.page.locator('[data-qa-id=adview_price] >> nth=0').innerText()
            data.date = await context.page.locator('[data-qa-id=adview_date] >> nth=0').innerText()
            data.description = await context.page.locator('[data-qa-id=adview_description_container] >> nth=0').innerText()
            // data.link = userData.link
        }else{
            context.log.info('not label product, so search or pagination.');
            let products = []
            // we are looking for product to be queued, let's write it down
            userData.label = 'product';
            const elements = context.page.locator('[data-qa-id=aditem_container]');
            const links = await elements.evaluateAll(elems => elems.map(elem => "https://www.leboncoin.fr"+elem.getAttribute('href')));
            // await context.enqueueRequest('https://www.leboncoin.fr/recherche?category=21&text=got&price=17-50', {test : 'test'}, false);
            links.forEach(async link => {
                await context.enqueueRequest(link, userData , false);
            })
            // data.products = products
        }
        context.log.info(`function ended`);
        return data;
    },
    "proxyConfiguration": {
        "useApifyProxy": true
    },
    "pseudoUrls": [
        {
            "purl": "https://www.leboncoin.fr[.*]"
        }
    ]
};

(async () => {
    // Run the actor and wait for it to finish
    const run = await client.actor("anchor/leboncoin").call(input);

    // Fetch and print actor results from the run's dataset (if any)
    console.log('Results from dataset');
    const { items } = await client.dataset(run.defaultDatasetId).listItems();
    items.forEach((item) => {
        console.dir(item);
    });
})();