coリ・ー・ン<2nd life

2006 09 22

rubyスクレイピングツールキット scrAPI

http://blog.labnotes.org/category/scrapi/

rubyスクレイピングして web情報を取得するのには、今まで正規表現xpath でやってたので、わりと面倒でした。で、ふと scrAPI というスクレイピングツールキットを知ったのですが、これがかなり便利そう。

このツールキットを使うと、CSS3 なセレクタ記述することで、要素を取得することができます。たとえばとあるサイトリンクを全部取得したければ、

require 'rubygems'
require 'scrapi'
require 'open-uri'
require 'nkf'
require 'pp'

$KCODE = 'u'

links = Scraper.define do
  process "a[href]", "urls[]"=>"@href"
  result :urls
end.scrape(URI.parse('http://www.hatena.ne.jp/')).sort.uniq

pp links

な感じですっきり記述することができます。

たとえばはてなダイアリーキーワードページから、タイトル、ふりがな、URLカテゴリを取得したければこんな感じなコードで。

keyword_scrapper = Scraper.define do
  process 'span.title > a:first-child', :title => :text, :url => '@href'
  process 'span.furigana', :furigana => :text
  process 'ul.list-circle > li:first-child > a', :category => :text
  result :title, :furigana, :url, :category
end

html = NKF::nkf('-w -m0', open('http://d.hatena.ne.jp/keyword/%BA%B0%CC%EE%A4%A2%A4%B5%C8%FE').read)
pp keyword_scrapper.scrape(html, :parser => :html_parser)

結果は構造体として取得できるのでアクセスも楽々です。

#<struct
 title="紺野あさ美",
 furigana="こんのあさみ",
 url="/keyword/%ba%b0%cc%ee%a4%a2%a4%b5%c8%fe?kid=800",
 category="アイドル">

一つはまったのが、デフォルトhtml の構文解析では Tidyライブラリを使うのですが、このライブラリマルチバイト文字列をエンティティにしちゃうので困るという罠が…。そのため、scrape のオプションで pure ruby の HTMLParser を指定することで回避できます。

追記

HTMLParser を指定せずとも、tidyオプション

scrape(html, :parser_options => {:char_encoding=>'utf8'})

と渡せばよしなにはからってくれます。コメントで教えてもらいました。


また様々な方法で Scraper を定義できるのでいろいろできそう。海外じゃやたら流行ってる rubyメタプログラミングを多用していてクラス定義などなどが行えます。

今まではスクレイピングには断然 perl のほうが便利だったのですが、この scrAPI を使いこなすだけで、だいぶ ruby でも便利になる気が!

歩行者歩行者 2006/09/24 03:26 こんにちは、「Gentoo Linux で個人サーバー」という
blog を書いております、歩行者と申します。
先ほどトラックバックさせていただきました。
scrAPI、すごく便利そうですね!
CSS セレクタで要素を指定するというアイディアは脱帽です。
素晴らしい情報ありがとうございます。

磊 2006/09/29 01:10 pp keyword_scrapper.scrape(html, :parser_options => {:char_encoding=>”utf8”)
とやるとマルチバイト文字列がエンティティになっちゃうのがなおるかもしれません。

EdwardEdward 2006/11/05 21:25 Well done!
[url=http://zrfswtqt.com/qptf/opuw.html]My homepage[/url] | [url=http://isyeppbp.com/sxcr/daks.html]Cool site[/url]

AliceAlice 2006/11/05 21:25 Great work!
<a href=”http://zrfswtqt.com/qptf/opuw.html”>My homepage</a> | <a href=”http://ginbkdhd.com/aetn/lotn.html”>Please visit</a>

EmmaEmma 2006/11/05 21:25 Good design!
http://zrfswtqt.com/qptf/opuw.html | http://bbexhclz.com/ihhz/nwxi.html

GlenGlen 2006/11/09 05:08 Great work!
[url=http://hyzmmtzh.com/aurb/gtwa.html]My homepage[/url] | [url=http://lshbwyuw.com/yugz/xdwi.html]Cool site[/url]

HowardHoward 2006/11/09 05:09 Great work!
<a href=”http://hyzmmtzh.com/aurb/gtwa.html”>My homepage</a> | <a href=”http://fcpyguad.com/poaz/lqdt.html”>Please visit</a>

AndyAndy 2006/11/09 05:09 Well done!
http://hyzmmtzh.com/aurb/gtwa.html | http://jzieayql.com/glwy/lxsg.html

Punk from TaiwanPunk from Taiwan 2007/01/29 23:20 dear sir,
the information about ”set encoding type for tidy” is helpful for me. Thank you very much.
I love Ruby and Ruby on Rails!

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証

プログラミングRuby 第2版 言語編
Ruby を始めようと思う人から玄人まで身近に置いておきたい一冊
Ruby on Rails入門―優しいRailsの育て方
Rails 入門書の決定版!あのくまくまーの独特な解説ですんなり読める!