2017-10-05 247 views
1

我試圖從頁面上抓取圖像,但如果該頁面未完全加載,頁面將返回佔位符源attr(需要約0.5秒才能完全加載)我將如何提出請求等待?節點js請求和cheerio等待頁面完全加載

試着做

function findCommonMovies(movie, callback){ 

    request('http://www.imdb.com/find?ref_=nv_sr_fn&q='+ movie +'&s=all', function (error, response, body) { 
     if (error){ 
      return 
     }else{ 
      var $ = cheerio.load(body); 
      var title = $(".result_text").first().text().split("(")[0].split(" ").join('') 
      var commonMovies = [] 
      // var endurl = $("a[name=tt] .result_text a").attr("href") 
      var endurl = $('a[name=tt]').parent().parent().find(".findSection .findList .findResult .result_text a").attr("href"); 


      request('http://www.imdb.com' + endurl, function (err, response, body) { 

       if (err){ 
        console.log(err) 
       }else{ 

        setInterval(function(){var $ = cheerio.load(body)}, 2000) 

        $(".rec_page .rec_item a img").each(function(){ 


        var title = $(this).attr("title") 
        var image = $(this).attr("src") 

        commonMovies.push({title: title, image: image}) 
        }); 
       } 
       callback(commonMovies) 
      }); 
     } 
    }); 

} 
findCommonMovies("Gotham", function(common){ 
    console.log(common) 
}) 

回答

0

的setTimeout(函數,millseconds等)將暫停你要多少秒。 setTimeout(function(){var $ = cheerio.load(body)},2000)

+0

不工作,我認爲它可能是異步.. –

+0

你做了2秒的等待時間?你有更長的時間嗎?這是唯一的javascript函數(除了setInterval),它允許你暫停。你可以嘗試聲明一個函數:function cheerio(){var $ = cheerio.load(body)}然後setTimeout(cheero,2000);也許它不喜歡匿名函數 – Hunter

+0

我很新的JavaScript,所以我可能是錯的,但不是在請求的響應('http://www.imdb.com'+ endurl,函數(錯誤,響應, body)已經被delcared了?我想也許有一種方法會等待 –

0

在我看來你的回調位於錯誤的地方,應該不需要任何計時器。當request()調用它的回調時,整個響應就緒,因此不需要定時器。

下面是用在正確的地方回調的代碼,同時也改變了,這樣他有一個錯誤說法使主叫方可以傳播和檢測錯誤:

function findCommonMovies(movie, callback){ 
    request('http://www.imdb.com/find?ref_=nv_sr_fn&q='+ movie +'&s=all', function (error, response, body) { 
     if (error) { 
      callback(error); 
      return; 
     } else { 
      var $ = cheerio.load(body); 
      var title = $(".result_text").first().text().split("(")[0].split(" ").join('') 
      var commonMovies = []; 
      // var endurl = $("a[name=tt] .result_text a").attr("href") 
      var endurl = $('a[name=tt]').parent().parent().find(".findSection .findList .findResult .result_text a").attr("href"); 
      request('http://www.imdb.com' + endurl, function (err, response, body) { 
       if (err) { 
        console.log(err) 
        callback(err); 
       } else { 
        var $ = cheerio.load(body); 
        $(".rec_page .rec_item a img").each(function(){ 
        var title = $(this).attr("title"); 
        var image = $(this).attr("src"); 
        commonMovies.push({title, image}); 
        }); 
        callback(null, commonMovies); 
       } 
      }); 
     } 
    }); 
} 

findCommonMovies("Gotham", function(err, common) { 
    if (err) { 
    console.log(err); 
    } else { 
    console.log(common) 
    } 
}); 

注意:這將僅訪問HTML標記服務由服務器提供您請求的網址。如果這些網頁的內容是通過瀏覽器的Javascript插入的,那麼這些內容就不會出現在您看到的內容中,並且不會出現任何延遲。這是因爲cheerio不運行瀏覽器JavaScript,它只是解析服務器最初發送的HTML。要運行瀏覽器Javascript,您需要一個比cheerio提供的更完整的瀏覽器引擎,例如PhantomJS,它將實際運行該頁面的Javascript。