2016-06-27 12 views
0

私は、このウェブサイトの記事タイトルとURLを取得する単純なWebスクレーパーを作成しました:http://espn.go.com/college-football/。ただし、スクレーパーはサイトからのすべての記事ではなく、46-50件の記事を返すだけです。私はcheerioが使用するCSSセレクタを変更しようとしましたが、それが掻く記事の数に関して何も変わりません。ここで私が使用しているコードは次のとおりです。Nodejsを使用したWebスクレイピング

var fs = require('fs'); 
var request = require('request'); 
var cheerio = require('cheerio'); 
var mongo = require('mongoskin'); 
var db = mongo.db("mongodb://localhost:27017/test", { native_parser: true }); 


url = 'http://espn.go.com/college-football/'; 

function Headline(title, link) { 
    this.Title = title; 
    this.link = link; 
} 

request(url, function (error, response, html) { 
    if (!error) { 
     var $ = cheerio.load(html); 

     var result = []; 

     // Grab the articles titles/url 
     $('.text-container h1 a.realStory', '#news-feed-content').each(function (i, elem) { 
      console.log($(elem).text(), elem.attribs.href); 
      var articleObject = new Headline($(elem).text(), elem.attribs.href); 
      result.push(articleObject); 
     }); 
    } 

    fs.writeFile('espn_articles.json', JSON.stringify(result, null, 4), function (err) { 

     console.log('File successfully written! - Check your project directory for the output.json file'); 

    }) 

    db.collection('articles').insert(result, function (error, record) { 
     if (error) throw error; 
     console.log("data saved"); 
    }); 
}); 
+0

スクロールダウンページは '無限scroll'ロード – charlietfl

+0

@charlietflに基づいて、追加されますか?私はページがオートスクロールすることを知っていますが、記事をスクロールダウンさせる方法を知りたいと思います。 –

答えて

2

Osmosisを使用した例です。あなたは手の込んだコンテンツが表示されます

osmosis('http://espn.go.com/college-football/') 
    .find('#news-feed-content .text-container') 
    .set({ 
     author: '.author', 
     category: '.category-link', 
     title: '.realStory', 
     link:  '[email protected]', 
     blurb: 'p' 
    }) 
    .follow('[email protected]') 
    .set({ 
     date: '.article-meta @data-date', 
     images: [ 'picture @srcset' ], 
     content: '.article-body' 
    }) 
    .data(function (article) { 
     /* 
     { author: '...', 
      category: '...', 
      title: 'Harbaugh, Michigan reel in Florida OL Herbert', 
      link: '...', 
      blurb: 'Jim Harbaugh and Michigan have landed another recruit from SEC country in Kai-Leon Herbert of Florida.', 
      date: '2016-07-06T17:25:09Z', 
      images: [ '...', '...' ], 
      content: '...' 
     } 
     */ 

     db.collection('articles').insert(article, function (error, record) { 
      // ... 
     }); 
    }) 
    .log(console.log) 
    .error(console.log) 
    .debug(console.log); 
2

あなたはそれがより多くの記事をレンダリングAPI呼び出しのたびになることがわかりますクロムのdevのツールを使ってページを見てみましょう。ここにURLがあります:http://cdn.espn.go.com/core/now?render=true&partial=nowfeed&xhr=1&sport=ncf&offset=0&device=desktop&userab=8

ここでは、オフセットパラメータがページネーションに使用されているとします。スクレーピングがとても良く

はそれが役に立てば幸い最初の許可を求めるために、特定の例では「違法」であることを念頭に置いて

てください!

+0

ありがとう@Sam。私はそれらに連絡し、プロジェクトを開始する前にコンテンツを掻き集めることができることを確認しました。私は記事をスクロールダウンさせる方法をまだ分からないと思う。私はPhantomJSのようなものを実装すべきですか? –

+0

こんにちは、この場合、あなたはする必要はありません。 URLのオフセット値の代わりに変数を使用するだけです。がんばろう – Sam

関連する問題