政策情報学部
渡辺恭人
riho-m@cuc.ac.jp
作業①:前回分を少し改造
1行目のplace = “
那覇
”の「那覇」の部分を他の地名
に変えてみる。
南西諸島地域以外の場合は、2行目も変更する必要
がある。
xmlFile =
http://weather.livedoor.com/forecast/rss/amedas/temp/
10/47.xml
http://weather.livedoor.com/weather_hacks/rss_feed_lis
t.htmlを参照する
気温をクリック
メニューバーの「表示」⇒「ソース」をクリックして確認
別のサンプル
もう少し凝った表示にする
文字のサイズを大きくする
文字に色をつける
背景色を設定する
日時も表示する
ブラウザの機能を利用する
HTA(HyperText Applicatio
ns)化する。
クリックすると再読み込み
サンプル
<html><head>
<hta:application border="dialog" scroll="no">
<meta http-equiv="content-type" content="text/html; charset=Shift_JIS"> <title>今の気温は?</title> <script language="VBScript"> Call Window.ResizeTo(250,120+30) place = "船橋" Sub Window_onload '処理準備 xmlFile = "http://weather.livedoor.com/forecast/rss/amedas/temp/3/12.xml"
Set xmlDoc = CreateObject("Microsoft.XMLDOM") xmlDoc.async = False
returnCode = xmlDoc.Load(xmlFile) If returnCode = False Then
MsgBox("読み込みエラー") Else
サンプル
'データ取得Set titleNodes =
xmlDoc.selectNodes("//item[title=contains(title,'"&place&"')]/title")
'整形
line = titleNodes(0).text
If line = "" Or titleNodes.length > 1 Then line = "エラー"
Else
pointer = InStr(line, "]")
Document.title = Left(line, pointer) line = Right(line, Len(line)-pointer)
nowtime.innerHTML = Date & " " & Time End If
'出力処理
cont.innerHTML = line End If
Sub cont_onClick location.reload() End Sub </script> <style type="text/css"> body {background-color:#808080} div#cont {font-size:60px;font-weight:bold;color:#ffff6a;text-align:center;cursor:hand} div#nowtime {color:#FFF;text-align:right} </style> </head> <body> <div id="cont"></div> <div id="nowtime"></div> </body> </html>
作業②
sample5.htaを書き換えて、別の場所の気温が表示でき
るようにする
まず、
http://weather.livedoor.com/weather_hacks/rss_feed_lis
t.html を見て、見たい地方の気温のRSSファイルへのリン
クを取得する
place = ”船橋” を変更
応用
天気予報Webクライアントを作る
表示はシンプル
天気を示す画像も加える
元になる情報の場所(東京の場合)
http://weather.livedoor.com/forecast/rss/13/63.xml
他の地域も、
http://weather.livedoor.com/weather_hacks/rss_feed_lis
t.html から選べる
ポイント
天気予報だから、翌日(明日)の情報を取得したい
①「明日の日付」を文字列で作成
②明日の日付に対応した”item/title”の行を探す。
”item/title”のitem下にあるdescriptionを天気予報の文章として
抜き出す
”item/title”のitem下にあるimage/urlのGIF画像URLを抜き出
して、天気画像とする
①「明日の日付」を文字列で作成
VBScriptで作成
nextDate = DateAdd("d",1,Date)
今日の日付の「日」に1を加える
tomorrow =
MonthName(Month(nextDate))&Day(nextDate)&"日
("&WeekdayName(Weekday(nextDate),True)&")“
明日の日付の文字列をMonthName関数とDay関数と
WeekdayName関数の出力をつないで作成
②明日の日付に対応した”item/title”の行を
探す
”item/title”のitem下にあるdescriptionを天気予報の文
章として抜き出す
Set descriptNodes =
xmlDoc.selectNodes("//item[title=contains(title,'"&tom
orrow&"')]/description")
”item/title”のitem下にあるimage/urlのGIF画像URLを
抜き出して、天気画像とする
Set imgNode =
xmlDoc.selectNodes("//item[title=contains(title,'"&tom
orrow&"')]/image/url")
プログラム
<html><head>
<hta:application border="dialog" scroll="no">
<meta http-equiv="content-type" content="text/html; charset=Shift_JIS">
<title>明日の天気</title>
<script language="VBScript">
Call Window.ResizeTo(200,150+30)
nextDate = DateAdd("d",1,Date)
tomorrow = MonthName(Month(nextDate))&Day(nextDate)&"日
("&WeekdayName(Weekday(nextDate),True)&")"
プログラム
Sub Window_onload '処理準備
xmlFile = "http://weather.livedoor.com/forecast/rss/13/63.xml" '東京 Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
returnCode = xmlDoc.Load(xmlFile) If returnCode = False Then
MsgBox("読み込みエラー") Else Call xmlDoc.setProperty("SelectionLanguage","XPath") 'データ取得 Set descriptNodes = xmlDoc.selectNodes("//item[title=contains(title,'"&tomorrow&"')]/description ") Set imgNode = xmlDoc.selectNodes("//item[title=contains(title,'"&tomorrow&"')]/image/url")