NokogiriでScrapingしてみた

Nokogiriって何?

Nokogiriとは、Rubyで書かれたHTMLやXMLなどのパーサーで、以下の特徴を持っています。(公式サイトから抜粋)

  1. XPath support for document searching
  2. CSS3 selector support for document searching
  3. XML/HTML builder
  4. Drop in replacement for Hpricot (though not bug for bug)

公式サイトやリポジトリは以下になります。

インストール

Macの場合
$ sudo gem install nokogiri
Password:
Building native extensions.  This could take a while...
Successfully installed nokogiri-1.2.1
1 gem installed
Installing ri documentation for nokogiri-1.2.1...
Installing RDoc documentation for nokogiri-1.2.1...
Ubuntuの場合

Macの場合の同様にインストールする。

$ sudo gem install nokogiri
[sudo] password for kouichi: 
Building native extensions.  This could take a while...
ERROR:  Error installing nokogiri:
        ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb install nokogiri
checking for #include <libxml/xmlversion.h>
... no
need libxml
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby1.8


Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/nokogiri-1.1.1 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/nokogiri-1.1.1/ext/nokogiri/gem_make.out

残念ながら上記のエラーが発生して失敗する。ぐぐってみると以下のブログのエントリに情報が載っていた。

どうやらlibxml2-devとlibxsltl-devをインストールしてあげる必要があるみたい。ってことでインストールする。

$ sudo aptitude install libxml2-dev libxslt1-dev

その後、nokogiriをインストールする。

$ sudo gem install nokogiri
Building native extensions.  This could take a while...
Successfully installed nokogiri-1.1.1
1 gem installed
Installing ri documentation for nokogiri-1.1.1...
Installing RDoc documentation for nokogiri-1.1.1...

今度はちゃんとインストールに成功した。

ってことで使ってみる

はてブのホットエントリからタイトル、ブックマーク数、リンクを取り出して表示するサンプルを作ってみる。とりあえずこんな感じかな。

#!/usr/bin/ruby
# -*- coding: utf-8 -*-

require 'rubygems'
require 'nokogiri'
require 'open-uri'

url = "http://b.hatena.ne.jp/hotentry"
html = Nokogiri::HTML(open(url), nil, 'UTF-8')

html.search('//ul[@class="hotentry"]/li/div[@class="entry-body"]').each do |entry|
  link = entry.search('h3/a')
  puts link.text.strip + "(" + entry.search('ul[@class="entry-info"]/li[@class="users"]//a').text + ")\n"
  puts link.search('@href').text + "\n"
  puts "\n"
end

で、実際に実行するとこんな感じで出力される。

$ ruby hotentry.rb
Twitter中毒の夫に不快感 : 恋愛・結婚・離婚 : 発言小町 : 大手小町...(437 users)
http://komachi.yomiuri.co.jp/t/2009/0226/227267.htm?g=04

世界の有名教授の授業がオンラインで聴講し放題!『Academic Earth』...(402 users)
http://www.ideaxidea.com/archives/2009/02/academic_earth.html

はてなブックマークニュース(278 users)
http://b.hatena.ne.jp/articles

Academic Earth - Video lectures from the world's top scholars(284 users)
http://academicearth.org/

(以降、省略)