Ruby/scrAPIを試してみる
22 Jan 2007RSS翻訳のWebサービスあって良いと思うのに、どうも無さそう?。
RSS ⇒ 翻訳サイト ⇒ 翻訳RSS
ということできれば良いけど、WebAPIを公開してる翻訳サービスって意外に少ない。。WebserviceX.NET :: Web Servicesってとこが公開してるけど、SOAPだしちょい微妙&scrAPIを使ってみたい。ということで普通の翻訳サービスをスクレイピングして無理矢理やる。
まずscrAPIをインストールgem install scrAPI gem install tidy
scrAPIのチートシートがあったので参考にしつつ。もしこういうHTMLを読み込むとしたら
<div.post_show> <h2><a href="http://hoge">タイトル~</a></h2> <h5>2006-12-01</h5> <div> 本文とか </div> </div>こんな感じでできる。
require 'rubygems'
require 'scrapi'
require 'open-uri'
$KCODE = 'u'
postScrape = Scraper.define do
process "h2 > a", :title => :text, :url =>"@href"
process "h5", :date => :text
result :title, :date, :url
end
bodyScrape = Scraper.define do
array :posts
process "div.post_show", :posts=>postScrape
result :posts
end
html = URI.parse('http://blog.tkmr.org/tatsuya/list/2')
p bodyScrape.scrape(html, :parser_options => {:char_encoding=>'utf8'})
にゃるへそ~、scrAPI良いね。
参考サイト
川o・-・)<2nd life - ruby のスクレイピングツールキット scrAPI