スクレイピング、例外処理のメモ。
処理系はgauche。
使用したライブラリは、 HtmlPragのversion 0.16。
http://www.neilvandyke.org/htmlprag/htmlprag-0-16.scm
最新版はPLT Schemeベッタリとか書いてあるっぽい。
下のソースはライブドアクリップのとあるユーザーのページから、使われてるタグクラウドの部分をごっそり持ってくるもの。
(add-load-path "/c/scheme/site-lib") (use rfc.http) (use sxml.ssax) (use sxml.sxpath) (load "htmlprag-0-16.scm") (define svr "clip.livedoor.com") (define user "someusername") (guard (excp ((condition-has-type? excp <http-error>) (format #t "connection error~%") 'http-error)) ; (else (format #t "what's happen?") 'other-error)) (receive (status header body) (http-get svr (string-append "/clips/" user)) (let1 tags ((sxpath "//div[@id='htmltagcloud']") (html->shtml body)) (print tags))))
1行目はhtmlprag-0-16.scmを配置した場所をロードパスに追加。
gaucheでtry-catchに相当するのはguardっぽい。
他にもあった。
で、上の方で例外オブジェクト(schemeではコンディションというらしい。)をcatchして、下の方でtry。
catchは、cond文のように述語で判別して。
httpクライアントはここではhttp-get。
多値を返すのでreceiveで受ける。
で、html本体をhtmlpragのhtml->shtmlでパース。
返ってくるsxmlデータをsxpathで処理。
シンプルだ。
参考
yosaka