-1

我想从纳斯达克的网站创建一个可刷新的股息历史查询,这里是苹果的 URL ( http://www.nasdaq.com/symbol/aapl/dividend-history )

当我尝试运行 Excel 的本机“新 Web 查询”时,股息历史表似乎不是一个可选选项。将此信息提取到电子表格中的最佳方法是什么?

如果可能的话,如果可以参考某个单元格中的代码运行查询,那也将非常有帮助。

任何意见,将不胜感激。

编辑:我正在专门寻找股息及其公告日期,只能在纳斯达克网站上找到这些信息。如果有一个更适合 excel 的替代方案,那也会有所帮助。

4

1 回答 1

0

试试看 - 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
于 2016-05-28T02:45:53.000 回答