天気予報
お天気Webサービス(http://weather.livedoor.com/weather_hacks/webservice.html)を使用してみます。
cityとdayの二つのパラーメータを渡すとXMLデータが返されるので、それをパースすればOKです。パーサーとしてHpricotを使用してみました。
#! ruby -Ks require 'hpricot' require 'open-uri' require 'kconv' id = 31 day = "tomorrow" doc = Hpricot( open("http://weather.livedoor.com/forecast/webservice/rest/v1?city=#{id}&day=#{day}").read ) puts (doc/:title).first.inner_html.tosjis puts (doc/:forecastdate).inner_html.tosjis puts (doc/:telop).inner_html.tosjis print "最高気温:".tosjis puts (doc/:max/:celsius).inner_html.tosjis print "最低気温:".tosjis puts (doc/:min/:celsius).inner_html.tosjis
実行結果
福島県 福島 - 明日の天気
Wed, 14 Jan 2009 00:00:00 +0900
晴時々曇
最高気温:6
最低気温:-1