Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
Can't include website link as a parameter while calling a function

I've created a script using request and cheerio libraries to fetch different post titles and their corresponding links from a website. The script appears to be doing fine. If you take a look at the script below, you can see that I've used getposts((item,link) => console.log({item,link})); to call the function.

Now, the question is:

How can I include startUrl (website link) as a parameter while calling the function keeping the rest of the logics as they are?

var request = require('request');
var cheerio = require('cheerio');

const startUrl = 'https://stackoverflow.com/questions/tagged/web-scraping';

function getposts(callback) {
  request(startUrl, function(error, response, html) {
    if (!error && response.statusCode == 200) {
      var $ = cheerio.load(html);
      $('.summary .question-hyperlink').each(function() {
        var items = $(this).text();
        var links = $(this).attr('href');
        return callback(items, links);
      });
    }
  });
}

getposts((item,link) => console.log({item,link}));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Create a new parameter for a url and pass in startUrl:

var request = require('request');
var cheerio = require('cheerio');

const startUrl = 'https://stackoverflow.com/questions/tagged/web-scraping';

function getposts(url, callback) {
  request(url, function(error, response, html) {
    if (!error && response.statusCode == 200) {
      var $ = cheerio.load(html);
      $('.summary .question-hyperlink').each(function() {
        var items = $(this).text();
        var links = $(this).attr('href');
        return callback(items, links);
      });
    }
  });
}

getposts(startUrl, (item,link) => console.log({item,link}));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda