検索パラメータが変更になっており、元のままだと、在来線の普通料金が出る。新幹線の大阪-東京の場合、現在のWebでは、これが検索結果のURL、
http://transit.loco.yahoo.co.jp/search/result?flatlon=&from=%E5%A4%A7%E9%98%AA&tlatlon=&to=%E6%9D%B1%E4%BA%AC&via=&shin=1&expkind=1&ym=201212&d=04&datepicker=&hh=14&m1=0&m2=1&type=1&ws=2&s=0&x=103&y=33&kw=%E6%9D%B1%E4%BA%AC
VBAで合成している最終的なURL:
http://transit.loco.yahoo.co.jp/search/result?from=%E5%A4%A7%E9%98%AA&to=%E6%9D%B1%E4%BA%AC
を、こうすると、新幹線の普通+特急(普通車)料金が出る。
http://transit.loco.yahoo.co.jp/search/result?&shin=1&expkind=1&from=%E5%A4%A7%E9%98%AA&to=%E6%9D%B1%E4%BA%AC
shin=1、expkind=1、以外のその他のパラメータの機能は不明
解かる範囲で(勝手に!)直してみたのが、これ、
Option Explicit
Sub FareLoco()
Const myYahoo = "http://transit.loco.yahoo.co.jp"
Const xHead = "出発地,目的地,経由地,片道"
Const xHeads = 1
Dim xLast As Long
Dim nn As Long
Dim IE As Object
Dim myURL As String
Dim myContent As String
Dim buf As String
Dim sST As String
Dim sDST As String
'ヤフー運賃検索(Yahoo!路線情報)
'Cells(1, "A").resize(1,4)=split(xHead,",")
xLast = Cells(Rows.Count, "A").End(xlUp).Row
For nn = xHeads + 1 To xLast
If (Cells(nn, "A").Value <> Empty) And (Cells(nn, "B").Value <> Empty) Then
sST = Encode_Uni2UTF(Cells(nn, "A").Value)
sDST = Encode_Uni2UTF(Cells(nn, "B").Value)
If (sST = "" Or sDST = "") Then MsgBox "セルに文字がありません。", 48: Exit Sub
'myURL = myYahoo & "/search/result?from=" & sST & "&to=" & sDST
'shin=1を指定しないと、在来線?
'expkind=1を指定しないと、普通料金だけ?
myURL = myYahoo & "/search/result?shin=1&expkind=1&from=" & sST & "&to=" & sDST
Set IE = CreateObject("InternetExplorer.Application")
With IE
'.Visible = True '表示制御
.Navigate myURL
Do While .Busy
DoEvents
Loop
Do Until .ReadyState = 4
DoEvents
Loop
myContent = .Document.body.innerHTML
'情報が取れなくなったときは、ここでログを取る
.Quit
End With
Set IE = Nothing
'結果出力
Cells(nn, "D").Value = PickUpString(myContent, "片道")
Else
Cells(nn, "D").Value = "ERROR:指定なし"
End If
Next nn
Columns("A:D").AutoFit
End Sub
Function PickUpString(ByVal strContent As String, SearchTxt As String)
Dim buf As String
Dim i As Long
Dim j As Long
buf = Mid$(strContent, InStr(1, strContent, SearchTxt, 1) + 2, 40)
i = InStr(1, buf, ">", 1) + 1
j = InStrRev(buf, "</S", , 1)
If i * j > 0 Then
PickUpString = Mid$(buf, i, j - i)
Else
PickUpString = "取得に失敗"
End If
End Function
Private Function Encode_Uni2UTF(ByRef strUni As String)
Dim buf As Variant
Dim tbuf As Variant
Dim n As Variant
Const CSET = "UTF-8"
Const ADTYPETEXT = 2
Const ADTYPEBINARY = 1
Dim ADOstrm As Object 'ADODB.Stream
On Error GoTo ErrHandler
Set ADOstrm = CreateObject("ADODB.Stream") 'New ADODB.Stream
ADOstrm.Open
ADOstrm.Type = ADTYPETEXT
ADOstrm.Charset = CSET
ADOstrm.WriteText strUni
ADOstrm.Position = 0
ADOstrm.Type = ADTYPEBINARY
ADOstrm.Position = 3
buf = ADOstrm.Read()
ADOstrm.Close
Set ADOstrm = Nothing
For Each n In buf
tbuf = tbuf & "%" & Hex(n)
Next
Encode_Uni2UTF = tbuf
Exit Function
ErrHandler:
If ADOstrm Is Nothing = False Then ADOstrm.Close
Set ADOstrm = Nothing
End Function
お礼
わざわざご確認をいただき、ありがとうございました。 実は、マクロができる人から「機能しない」と言われて慌てて質問してしまいましたが、私が確認したところ、A1とA2へのみ表示がされるということで理解致しました。 また、この機能だと特別料金は含まれない、一番安い金額で表示されるということがわかりました。