2017-03-04 72 views
0

我想爲一些內容報廢一個網站,但一切正常,但報廢文本僅適用於控制檯,但我想在瀏覽器上打印這些報廢的數據。我認爲我在處理回調的方式上做錯了什麼。任何人都可以幫忙嗎?nodejs網絡報廢和回調問題

我的代碼如下:從數據可用回調函數

app.get('/test', function(req, res) { 

     //All the web scraping magic will happen here 
    var url = 'https://www.mywebsite.com/path/to/abc'; 
    var allText; 
    var getTheText = function() { 
      request(url, function getText(error, response, html){ 

     // First we'll check to make sure no errors occurred when making the request 

     if(!error){ 
      // Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality 

      var $ = cheerio.load(html); 

      // Finally, we'll define the variables we're going to capture 

      var allText = $('body').children().find('p').text() 

       console.log('allText'); 
       console.log(allText); 
      return allText; 
     } 
     else { 
     } 

     //return result; 
    }); 
      console.log(allText); 

    } 

getTheText(); 
    console.log('gettheText is ' + getTheText()); 
    res.send(allText); 
}) 
+0

只是一個提示,不要處理cheerio wile處理請求。使用redis或kue將其推入後臺作業。一旦你完成了scraping,將結果推送到websocket或通過ws發送事件來獲取結果 – georoot

回答

0

發送響應。 請看下面的代碼:

app.get('/test', function(req, res) { 

    //All the web scraping magic will happen here 
    var url = 'https://www.mywebsite.com/path/to/abc'; 
    var allText; 
    var getTheText = function() { 
    request(url, function getText(error, response, html) { 

     // First we'll check to make sure no errors occurred when making the request 

     if (!error) { 
     // Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality 

     var $ = cheerio.load(html); 

     // Finally, we'll define the variables we're going to capture 

     var allText = $('body') 
      .children() 
      .find('p') 
      .text() 

     console.log('allText'); 
     console.log(allText); 
     // return allText; 
     res.send(allText); // Send response from here 
     } else {} 

     //return result; 
    }); 
    console.log(allText); 

    } 

    getTheText(); 
    console.log('gettheText is ' + getTheText()); 
    // res.send(allText); // Remove this line 
})