Unable to fetch results using callback

余生颓废 提交于 2019-12-25 00:52:46

问题


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});
    }
  });
}


来源:https://stackoverflow.com/questions/55597383/unable-to-fetch-results-using-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!