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

144
Visualizações
Can't apply the logic of grabbing next pages within a function

I've created a script using axios and cheerio to fetch different shop names and their associated links from yellowpages and then used those links to scrape phone and email from their inner pages. The script is doing fine.

What I wish to do now is use the next page link to keep grabbing the content from next pages as well. I just can't figure out how to apply the logic of parsing and using next pages within getLinks() function.

At the moment this is what I'm trying with:

const axios = require('axios');
const cheerio = require('cheerio');

const startUrl = 'https://www.yellowpages.com/search?search_terms=Pizza&geo_location_terms=San+Francisco%2C+CA';
const host = 'https://www.yellowpages.com';

const getLinks = async (url,host,callback) => {
      const { data } = await axios.get(url);
      const $ = cheerio.load(data);
      $('[class="result"] a.business-name').each(function(){
        let items = $(this).find('span').text();
        let links = host + $(this).attr("href");
        return callback(items,links);
      });
}

const fetchContent = async (shopName,shopLink,callback) => {
      const { data } = await axios.get(shopLink);
      const $ = cheerio.load(data);
      let phone = $('.contact > p.phone').eq(0).text();
      let email = $('.business-card-footer > a.email-business').eq(0).attr("href");
      return callback(shopName,shopLink,phone,email);
}

async function scrapeData() {
  getLinks(startUrl,host,function(itemName,link){
    fetchContent(itemName,link,function(shopName,shopLink,phone,email){
      console.log({shopName,shopLink,phone,email});
    });
  });
}

scrapeData();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Next page links are often in [rel=next] so it's usually something like this:

async function get(url) {
  const { data } = await axios.get(url);
  const $ = cheerio.load(data);
  return $
}

async function run(){
  let url = 'https://www.yellowpages.com/search?search_terms=Pizza&geo_location_terms=San+Francisco%2C+CA'
  let $ = await get(url)
  // doSomething($)
  let href = $('[rel=next]').attr('href')
  while(href){
    url = new URL(href, url).href
    $ = await get(url)
    // doSomething($)
    href = $('[rel=next]').attr('href')
  }
}
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