LoginSignup
8
8

More than 5 years have passed since last update.

cheerioでスクレイピング

Last updated at Posted at 2014-02-10

Nodejsでスクレイピングといえば、jsdomとかscrapperとか色々あると思うんですが、
今回はcheerioという早いとうわさのモジュールを使ってみます。

必要なモジュールをインストール

npm install request cheerio

プログラムを記述

var request = require("request");
var cheerio = require("cheerio");

var requestUrl = "http://example.com";

request({url: requestUrl},function(error,response,body){
  // If request succeed
  if (!error && response.statusCode == 200) {
      $ = cheerio.load(body);      
      console.log($("title").text());

  }  // If error occured
  else {
      if (error) {
          console.log("Error:" + error );
      }

  }
});

プログラムを実行

node app.js

該当ページのタイトルが取得できたはずです。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8