VBからブラウザを開き、たとえばヤフーファイナンスのランキングなどをVBプログラム内の変数に取り込みたいのですが、どのように行えばよいでしょうか?
参考になるようなソースはありますでしょうか?
'「参照設定」で「Microsoft Internet Controls」にチェック。
Private WithEvents IE As InternetExplorer
Private Sub Command1_Click()
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate2 "http://messages.yahoo.co.jp/ranking/stocks/postranking_daily.html"
IE.Visible = True
End Sub
Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim d(50, 1) As String
Set TDOBJ = IE.Document.body.getElementsByTagName("TD")
For Each a In TDOBJ
If a.innerText = "順位" Then f = 1
If c = 252 Then f = 0
If f = 1 Then
If c Mod 5 = 0 Then
d(c \ 5, 0) = a.innerText
End If
If c Mod 5 = 1 Then
d(c \ 5, 1) = b & a.innerText
End If
c = c + 1
End If
Next a
Set TDOBJ = Nothing
Set IE = Nothing
For i = 1 To 50
Debug.Print i, d(i, 0), d(i, 1)
Next i
End Sub
こっちの方がましかも。
'「参照設定」で「Microsoft Internet Controls」にチェック。
Private Sub Command1_Click()
Dim d(50, 1) As String
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate "http://messages.yahoo.co.jp/ranking/stocks/postranking_daily.html"
While .Busy Or .ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
For Each TB In .Document.getElementsByTagName("TABLE")
If (TB.Rows.Length = 51) And (TB.Rows(0).Cells.Length = 4) And (TB.Rows(0).Cells(0).innertext = "順位") Then
Exit For
End If
Next TB
For i = 1 To 50
d(i, 0) = TB.Rows(i).Cells(1).innertext
d(i, 1) = TB.Rows(i).Cells(2).innertext
List1.AddItem Format(i, "00") & " " & d(i, 0) & " " & d(i, 1)
Next i
Set TB = Nothing
.Quit
End With
End Sub
ツイート | ![]() |