Unable to fetch results using callback

2019-08-30 04:32发布

问题:

I've written a script in node using two different functions getPosts() and getContent() supplying callback within them in order to print the result calling a standalone function getResult(). The selectors defined within my script is flawless.

However, when I execute my script, It prints nothing. It doesn't throw any error either. I tried to mimic the logic provied by Neil in this post.

How can I make it a go?

I've written so far:

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

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

function getPosts(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");
        callback(items,links);
      });
    }
  });
}

function getContent(item,link,callback){
  request(link, function (error,response, html) {
    if (!error && response.statusCode == 200){
      var $ = cheerio.load(html);
      var proLink = $('.user-details > a').eq(0).attr("href");
      callback({item,link,proLink});
    }
  });
}

function getResult() {
  getPosts(function(item,link) {
    getContent(item,link,function(output){
      console.log(output);
    });
  });
}

getResult();

回答1:

The link value that you receive from getPosts is a relative link which means that the request fails. You can extract the hostname inside its own variable and create the full URL from the hostname + the relative link.

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

// ...

function getContent(item,link,callback){
  // Here we use the absolute URL
  request(host + link, function (error,response, html) {
    if (!error && response.statusCode == 200){
      var $ = cheerio.load(html);
      var proLink = $('.user-details > a').eq(0).attr("href");
      callback({item,link,proLink});
    }
  });
}