Yahoo PipesにおけるYQLの使い方 -超簡略版-
YQLは機能が豊富すぎて正直何が何だか分からない。というわけで、Yahoo Pipesで使う為の最低限の事柄のみ解説。
RSS加工の為にYQL Moduleを使う時はほぼこのパターン。
select (抜き出す要素) from (フォーマット) where url="(URL)"
html
select * from html where url="http://finance.yahoo.com/q?s=yhoo" and xpath="//div[@id='yfi_headlines']/div[2]/ul/li/a"
抜き出す要素にtagは使えない。ワイルドカードのみ。抜き出すtagは最後にxpathで指定する。xpathに関しては頑張ってぐぐれ。ていうかxpath使わないならfetch Page moduleでも使っとけばいい。
注意するのは文字化け率が高いという事。どうも文字コードに厳しいようだ。
(2011-02-27追記)
charsetが指定できるらしい。
select * from html where url="http://finance.yahoo.com/q?s=yhoo" and charset="utf-8" and xpath="//div[@id='yfi_headlines']/div[2]/ul/li/a"
rss
select * from rss where url="http://rss.news.yahoo.com/rss/topstories"
何も難しい事は無い。titleとlinkだけあればいいなんて時は、抜き出す要素で指定する。
select title,link from rss where url="http://rss.news.yahoo.com/rss/topstories"
読み込む件数を指定する事も出来る。
select * from rss where url="http://rss.news.yahoo.com/rss/topstories" limit 10
一端全部読み込んでTruncate ModuleかTail Module使ったっていいんだけど、item数が多くなるとLoad時間に露骨に差が出る。
xml
select * from xml where url="http://rss.news.yahoo.com/rss/topstories"
xmlの場合は要素指定が出来る。
select channel.title from xml where url="http://rss.news.yahoo.com/rss/topstories"
htmlstring
YQLでhtmlをscrapeすると結果が配列で返ってくる。基本的にそれで構わないが、scrape元のhtmlソースの出来が悪いと、配列で返されたら都合が悪い時も偶にある。そう言う時はhtmlstringを使うと普通にtextで返してくれる。
まずShow Community Tablesをクリックする。Community Tableというのは要するにユーザー自作のModule。
Community Tableを見えるようにしておかないと機能が有効にならない。後はhtmlの時と一緒。結果がtextで返ってくるってだけ。
select * from htmlstring where url="http://www.yahoo.com/" and xpath="//a"
読み込む時点で抜き出す要素が指定できるので、覚えればFetch PageとかFetch Detaよりは使い勝手は良い。