试试看 - VBA 网页抓取不是最优雅的,但所有工具都可以实现目标:
Option Explicit
Sub Test()
Call PasteNasdaqTickerTableHtmlToSheet(ThisWorkbook.Sheets("Sheet4"), "aapl")
End Sub
Sub PasteNasdaqTickerTableHtmlToSheet(ByRef ws As Worksheet, ByVal strTicker As String)
Dim strTableData As String
Dim objDataObject As Object
strTableData = GetTableHtmlFromUrl("http://www.nasdaq.com/symbol/" & strTicker & "/dividend-history", "quotes_content_left_dividendhistoryGrid")
Set objDataObject = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
With objDataObject
.SetText strTableData
.PutInClipboard
End With
ws.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False
End Sub
Function GetTableHtmlFromUrl(ByVal strUrl As String, ByVal strTableId As String) As String
Dim objHtmlReader As Object
Dim strDocumentHtml As String
Dim objHtmlParser As Object
Dim objHtmlTables As Object
Dim strTableHtml As String
Dim intTableCounter As Integer
On Error GoTo ErrHandler
'get document html
Set objHtmlReader = CreateObject("MSXML2.XMLHTTP")
With objHtmlReader
.Open "GET", strUrl, False
.Send
strDocumentHtml = .ResponseText
End With
Set objHtmlReader = Nothing
'return all html if no id supplied
If strTableId = "" Then
GetTableHtmlFromUrl = strDocumentHtml
Exit Function
End If
'now parse html for tables
Set objHtmlParser = CreateObject("htmlfile")
objHtmlParser.body.innerHTML = strDocumentHtml
Set objHtmlTables = objHtmlParser.getElementsByTagName("table")
'iterate tables and check id against our input
For intTableCounter = 1 To objHtmlTables.Length
If objHtmlTables.Item(intTableCounter).getAttribute("id") = strTableId Then
strTableHtml = objHtmlTables.Item(intTableCounter).outerHTML
Exit For
End If
Next intTableCounter
Set objHtmlTables = Nothing
Set objHtmlParser = Nothing
'return table html
GetTableHtmlFromUrl = strTableHtml
Exit Function
ErrHandler:
Debug.Print Err.Number & " : " & Err.Description
Set objHtmlReader = Nothing
GetTableHtmlFromUrl = ""
End Function