Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!