我正在尝试使用Excel中的VBA从此site获取数据。我试图做的和有效的是使用像这样的InternetExplorer对象:
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Navigate "http://zertifikate.finanztreff.de"
IE.document.getElementById("USFsecuritySearchDropDown").Value = "DE000BP5TBQ2"
IE.document.getElementById("USFsecuritySearchDropDownForm").submit
Do While IE.Busy Or IE.readyState <> 4 'wait until page is loaded
Application.Wait DateAdd("s", 1, Now)
Loop
MsgBox IE.document.getElementById("BP5TBQ~30~5").innerHTML
然而,这工作非常缓慢,并没有得到正确的结果。我怀疑有时它不会等到网页加载。我试图寻找答案,然后找到this answer on stackoverflow。现在我想弄清楚如何使用MSXML2和MSHTML重写我的宏。到目前为止,我能够做到这一点:
Dim IE As MSXML2.XMLHTTP60
Set IE = New MSXML2.XMLHTTP60
IE.Open "GET", "http://zertifikate.finanztreff.de", False
IE.send
While IE.ReadyState <> 4
DoEvents
Wend
Dim HTMLDoc As MSHTML.HTMLDocument
Dim htmlBody As MSHTML.htmlBody
Set HTMLDoc = New MSHTML.HTMLDocument
Set htmlBody = HTMLDoc.body
htmlBody.innerHTML = IE.responseText
HTMLDoc.getElementById("USFsecuritySearchDropDown").Value = "DE000BP5TBQ2"
请问,为什么HTMLDoc有方法getElementById而htmlBody没有?我怎么能提交表格“USFsecuritySearchDropDownForm”。我试过这个:
HTMLDoc.getElementById("USFsecuritySearchDropDownForm").submit
,但它总是在我的默认浏览器中打开新窗口,我想隐藏它。 在我看来,我错过了XMLHTTP60和MSHTML.HTMLDocument之间的区别。 如果你能帮助我或至少告诉我在哪里可以找到这些信息,我会非常感激...
答案 0 :(得分:9)
XMLHTTP向Web服务器发送http请求并接收响应。 MSHTML接收一个字符串并呈现HTML。当您一起使用它们时,XMLHTTP获取Web服务器响应,MSHTML将该响应放入您可以使用的表单中。
我认为你不需要提交任何东西。如果你去网站并输入代码,你会看到像
这样的页面其中有自动收报机。您可以直接“获取”该URL,并从返回的html中获取所需的任何信息。这个例子得到我认为的股票价格。
Sub GetPrice()
Dim xHttp As MSXML2.XMLHTTP
Dim hDoc As MSHTML.HTMLDocument
Dim hDiv As HTMLDivElement
Dim hTbl As HTMLTable
Const sTICKER As String = "DE000BP5TBQ2"
Set xHttp = New MSXML2.XMLHTTP
xHttp.Open "GET", "http://zertifikate.finanztreff.de/dvt_einzelkurs_uebersicht.htn?seite=zertifikate&i=22558284&suchbegriff=" & sTICKER & "&exitPoint="
xHttp.send
Do Until xHttp.readyState = 4
DoEvents
Loop
If xHttp.Status = 200 Then
Set hDoc = New MSHTML.HTMLDocument
hDoc.body.innerHTML = xHttp.responseText
'Get the third TD in the first TABLE in the first DIV whose class is 'tape'
Set hDiv = hDoc.getElementsByClassName("tape").Item(0)
Set hTbl = hDiv.getElementsByTagName("table").Item(0)
Debug.Print hTbl.getElementsByTagName("td").Item(2).innerText
End If
End Sub
发布示例
Sub GetPriceByPost()
Dim xHttp As MSXML2.XMLHTTP
Dim hDoc As MSHTML.HTMLDocument
Dim hDiv As HTMLDivElement
Dim hTbl As HTMLTable
Const sTICKER As String = "i=635957"
Set xHttp = New MSXML2.XMLHTTP
xHttp.Open "POST", "http://fonds.finanztreff.de/fonds_einzelkurs_uebersicht.htn"
xHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xHttp.send sTICKER
Do Until xHttp.readyState = 4
DoEvents
Loop
If xHttp.Status = 200 Then
Set hDoc = New MSHTML.HTMLDocument
hDoc.body.innerHTML = xHttp.responseText
'Get the third TD in the first TABLE in the first DIV whose class is 'tape'
Set hDiv = hDoc.getElementsByClassName("tape").Item(0)
Set hTbl = hDiv.getElementsByTagName("table").Item(0)
Debug.Print hTbl.getElementsByTagName("td").Item(2).innerText
Else
Debug.Print xHttp.statusText
End If
End Sub