Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Multiple selector search via puppeteer library

I use puppeteer to get data about the store. I search using the p.shop-page-content__text_large, span.shop-list-item__address selectors, but I ran into such a problem that only one of them can be present on the page. I tried to solve the problem in the following way, but it does not work. Tell me how can this be fixed?

const puppeteer = require('puppeteer');

const browser = await puppeteer.launch({
    headless: false,
    slowMo: 150,
});

const cities = [{'CITY': 'Town1', 'LINK': '/shops/town1/'}, {'CITY': 'Town2', 'LINK': '/shops/town2/'}];

async function getData(page, selector) {
    return await page.$$eval(selector, info => info.map((data) => {
        let str     = data.textContent.trim(),
            from    = str.search(','),
            to      = str.length;
    
        return {
            'COUNTRY': 'unknow',
            'STREET' : str.substring(from, to)
        }
    }));
}

const result = [];

for (let val of cities) {
    console.log(val.LINK, val.CITY);

    const page = await browser.newPage();
    await page.goto('https://www.example-site.ru' + val.LINK);

    data = await page.waitForFunction('.shop-page-content').then(async() => {
        console.log('ok');
        return await getData(page, 'p.shop-page-content__text_large');
    }).catch(async (e) => {
        console.log('fail');
        
        await page.waitForSelector('.shops-info__section');
        return await getData(page, 'span.shop-list-item__address');
        // result.push(data);
    });

    result.push(data);
    await browser.close();
}

console.log(result);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It turned out like this:

const browser = await puppeteer.launch({
    headless: false,
    slowMo: 150,
});
const cities = [{'CITY': 'Town1', 'LINK': '/shops/town1/'}, {'CITY': 'Town2', 'LINK': '/shops/town2/'}];
const page   = await browser.newPage();
const result = [];

for (let val of cities) {
    await page.goto('https://www.example-site.ru' + val.LINK);

    const list = await page.evaluate(() => {
        const data     = []; 
        const elements = document.querySelectorAll('p.shop-page-content__text_large').length 
                        ? document.querySelectorAll('p.shop-page-content__text_large')
                        : document.querySelectorAll('span.shop-list-item__address'); 

        for (const element of elements) {
            data.push(element.innerText);
        }

        return data;
    });

    result.push({
        link: val.LINK,
        city: val.CITY,
        list
    })
}

await browser.close();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda