Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponpoko04/875dbe62acfbaa69eccddff5794f77a2 to your computer and use it in GitHub Desktop.
Save ponpoko04/875dbe62acfbaa69eccddff5794f77a2 to your computer and use it in GitHub Desktop.
# 定数宣言
set URL "http://transit.yahoo.co.jp/traininfo/area/4/" -Option Constant
set OUTPUT_HTML ".\output.html" -Option Constant
set HTML_FOR_SHOW ".\rail.html" -Option Constant
set MODE_LOAD 0 -Option Constant
set MODE_RELOAD 1 -Option Constant
# 実行モードを決定します
$mode = $MODE_LOAD
if ($args.Length -gt 0 -and $args[0] -eq $MODE_RELOAD) {
$mode = $MODE_RELOAD
}
# サーバーからデータ取得します
if ($mode -eq $MODE_RELOAD) {
$wc = New-Object Net.WebClient
$html = [Text.Encoding]::UTF8.GetString($wc.DownloadData($URL))
} else {
if (Test-Path $OUTPUT_HTML) {
$html = Get-Content $OUTPUT_HTML -Encoding UTF8 | Out-String
} else {
$wc = New-Object Net.WebClient
$html = [Text.Encoding]::UTF8.GetString($wc.DownloadData($URL))
}
}
$html | Out-File $OUTPUT_HTML -Encoding utf8
# DOMにします
$parsedHtml = New-Object -com "HTMLFILE"
$parsedHtml.IHTMLDocument2_write($html);
$parsedHtml.Close()
# 出力します
"事故・遅延情報はありません" | Out-File $HTML_FOR_SHOW -Encoding utf8
$railsBlock = $parsedHtml.getElementById("mdAreaMajorLine")
$obj = @{}
$objs = foreach($elem in $railsBlock.children) {
# 「関東主要鉄道」ブロック配下をループします
if ($elem.attributes -eq $null) { continue }
if ($elem.attributes.getNamedItem("class").value -eq "labelSmall") {
# 鉄道社名
$companyName = $elem.innerText
} elseif ($elem.attributes.getNamedItem("class").value -eq "elmTblLstLine") {
# 鉄道1社内の各路線
$table = $elem.getElementsByTagName("table") | select -First 1
foreach($row in ($table.rows | select -Skip 1)) {
# 1路線内のセルをループして解析します
$row.Cells | foreach -Begin {
$obj = @{}
# 初期化
$railName = $null,$href = $null,$info = $null,$detail = $null
} -Process {
$cell = $_
switch($_.cellIndex) {
0 {
$railName = $cell.innerText
$href = $cell.children.item(0).attributes.getNamedItem("href").value
}
1 {
# 平常運転以外は2つ目のセル(「状況」)配下にタグが2つ以上あります
if ($cell.children.length -gt 1) {
$tmp = $cell.children.item(1).innerText
switch($tmp) {
"平常運転" {
$info = $tmp + "に戻ったようです"
}
"列車遅延" {
$info = $tmp + "しちゃってるみたいです・・・"
}
"運転状況" {
$info = $tmp + "を確認した方がいいかも"
}
"交通障害情報" {
$info = $tmp + "発令なう"
}
}
#$info = $cell.children.item(1).innerText
}
}
2 { $detail = $cell.innerText }
}
} -End {
if ($info -ne $null) {
$obj += @{
CompanyName = $companyName
Info = $railName + " " + $info
Detail = $detail
URL = $href
}
[pscustomobject]$obj
}
}
}
# こちらでも同様に動くし短いが、どこをセットしているのかわかりずらい
#$elem.getElementsByTagName("tr") | foreach -Process {
# if ($_.children.item(1).children.length -gt 1) {
# # 平常運転以外は2つ目のセル(「状況」)配下にタグが2つ以上あります
# $obj += @{
# CompanyName = $companyName
# Info = $_.children.item(0).innerText + " " + $_.children.item(1).children.item(1).innerText
# URL = $_.children.item(0).children.item(0).attributes.getNamedItem("href").value
#      Detail = $_.children.item(2).innerText
# }
# [pscustomobject]$obj
# $obj = @{}
# }
#}
}
}
$objs | foreach -Begin {
if ($objs.Length -gt 0) {
$null | Out-File $HTML_FOR_SHOW -Encoding utf8
}
} -Process {
echo ("<div><a href=`"{0}`" target=`"_blank`">{1} : {2}</a></div>" -f $_.URL, $_.CompanyName, $_.Info) |
Out-File -Append $HTML_FOR_SHOW -Encoding utf8
}
@ponpoko04
Copy link
Author

もうちょっと処理を分割しよう。
関数定義の練習にもなるし。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment