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
該当ページのタイトルが取得できたはずです。