我想在纳斯达克的网站上为股息历史创建一个可刷新的查询,Apple的URL在这里(http://www.nasdaq.com/symbol/aapl/dividend-history)
当我尝试运行Excel的原生“新Web查询”时,股息历史表似乎不是可选择的选项。将此信息提取到电子表格的最佳方法是什么?
如果可能的话,如果可以参考某个单元格中的股票代码来运行查询,那么它也会非常有用。
任何建议都将受到赞赏。
编辑:我正在寻找股息及其公告日期,并且只能在纳斯达克的网站上找到这些信息。如果有一个更好的替代方案,也会有所帮助。
答案 0 :(得分: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