2017-06-28 90 views
0

我想讓我的網站刮板從網站上拉出每篇文章的標題(https://hypebeast.com/footwear),但我似乎無法得到任何東西,但未定義或一些非常麻煩的錯誤。我究竟做錯了什麼?下面是我的代碼片段:使用NodeJS和Cheerio的網頁抓取

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

     var titles = []; 

     request('https://hypebeast.com/footwear', function(err, resp, body) { 
      var $ = cheerio.load(body); 
      $('.title').each(function(){ 
       var title = $(this).attr('span'); 
       titles.push(title); 
      }); 

      console.log(titles); 

     }); 

這裏的錯誤: http://imgur.com/chB9v6h

+0

請出示您的錯誤。 – Aswin

回答

0

這不是一個Cheerio讀書問題。 我打開網站,我看到DOM結構是不同的。 所以找你必須使用腳本像這樣的東西:

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

 
     var titles = []; 
 

 
     request('https://hypebeast.com/footwear', function(err, resp, body) { 
 
      var $ = cheerio.load(body); 
 
      $('.title').each(function(){ 
 
       var title = $(this).children("h2").children('span').text(); 
 
       titles.push(title); 
 
      }); 
 

 
      console.log(titles); 
 

 
     });

+0

啊,謝謝,現在工作:) – newang