我正在为我的股票构建一个excel文件,我想从许多网址中获取很多数据。我用powerquery解决了这个问题,但是它非常慢,所以我想改用vba脚本。
我可以将其用于单个网址,但不适用于多个网址。
Sub ScrapDataUsingXMLHTTP()
Dim XMLRequest As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLDiv As MSHTML.IHTMLElement
Dim HTMLTable As MSHTML.IHTMLElement
Dim URLs As String
Dim i As Integer
XMLRequest.Open "GET", "https://www.boerse.de/aktien/Microsoft-Aktie/US5949181045", False
XMLRequest.send
If XMLRequest.Status <> 200 Then
MsgBox XMLRequest.Status & " - " & XMLRequest.statusText
Exit Sub
End If
HTMLDoc.body.innerHTML = XMLRequest.responseText
Set HTMLDiv = HTMLDoc.getElementById("content_container")
Set HTMLTable = HTMLDiv.getElementsByTagName("table")(0)
'Debug.Print HTMLTable.className
WriteTableToWorksheet HTMLTable
结束子
这有效,但是当我要引用表内容(URL,存储在表中)时,它不起作用。如何解决?
Sub ScrapDataUsingXMLHTTP()
Dim XMLRequest As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLDiv As MSHTML.IHTMLElement
Dim HTMLTable As MSHTML.IHTMLElement
Dim URLs As String
Dim i As Integer
XMLRequest.Open "GET", ActiveSheet.ListObjects("Tabelle1").DataBodyRange(1, 2).Select, False
XMLRequest.send
If XMLRequest.Status <> 200 Then
MsgBox XMLRequest.Status & " - " & XMLRequest.statusText
Exit Sub
End If
HTMLDoc.body.innerHTML = XMLRequest.responseText
Set HTMLDiv = HTMLDoc.getElementById("content_container")
Set HTMLTable = HTMLDiv.getElementsByTagName("table")(0)
'Debug.Print HTMLTable.className
WriteTableToWorksheet HTMLTable
答案 0 :(得分:0)
我修复了它,终于发现了我的错误。我必须从表引用中删除“ .select”。我现在可以使用一些I / J变量从表中遍历URL。还是谢谢你。