简体   繁体   中英

Get data from HTML Table to Excel with VBA

I am trying to get historical data from Nasdaq directly into excel. I have managed to call up the website, change the "FromDate" input and hit the search button, but I can't scrape the table "historical Output" in a nice format to Excel - please help.

MY VBA CODE

Sub OMX_data()

    Dim URL As String
    Dim ie As InternetExplorer

    Set ie = New InternetExplorer
    ie.Visible = True

    ie.navigate ("http://www.nasdaqomxnordic.com/indexes/historical_prices?Instrument=DK0060368991")

    Do
        DoEvents
    Loop Until ie.readyState = 4

    ie.document.all("FromDate").Value = "2018-01-01"

    Set search_button = ie.document.getElementsByClassName("doSearch")
    search_button(0).Click

End Sub

MY HTML

<div id="historicalOutput"><table id="historicalTable" class="tablesorter tablesorter-default" border="0" cellpadding="0" cellspacing="0" role="grid">

Adding on to your code supplied, something like so

......search_button(0).Click

    Dim d As MSHTML.HTMLDocument
    Set d = ie.document

    Dim e As MSHTML.HTMLTable

    Set e = d.getElementById("historicalTable")

You can then look at the intellisense for this object e and get properties like

e.rows(100).innertext

e.rows.length

Have a look and build a loop out of it

I also changed Loop Until ie.readyState = 4 And Not ie.Busy

Try the script below to get the tabular content from that webpage using the date you wanna start from.

Sub FetchTable()
    Const postUrl$ = "http://www.nasdaqomxnordic.com/webproxy/DataFeedProxy.aspx"
    Dim Http As New XMLHTTP60, Html As New HTMLDocument
    Dim sPostData$, dateFrom$, dateupto$, C&, R&

    dateFrom = "2018-01-01"
    dateupto = "2020-04-02"

    sPostData = "xmlquery=%3Cpost%3E%0A%3Cparam+name%3D%22Exchange%22+value%3D%22NMF%22%2F%3E%0A%3Cparam+name%3D%22SubSystem%22+value%3D%22History%22%2F%3E%0A%3Cparam+name%3D%22Action%22+value%3D%22GetDataSeries%22%2F%3E%0A%3Cparam+name%3D%22AppendIntraDay%22+value%3D%22no%22%2F%3E%0A%3Cparam+name%3D%22Instrument%22+value%3D%22DK0060368991%22%2F%3E%0A%3Cparam+name%3D%22FromDate%22+value%3D%22" & dateFrom & "%22%2F%3E%0A%3Cparam+name%3D%22ToDate%22+value%3D%22" & dateupto & "%22%2F%3E%0A%3Cparam+name%3D%22hi__a%22+value%3D%220%2C1%2C2%2C4%2C21%2C8%2C10%2C11%2C12%22%2F%3E%0A%3Cparam+name%3D%22ext_xslt%22+value%3D%22%2FnordicV3%2Fhi_table.xsl%22%2F%3E%0A%3Cparam+name%3D%22ext_xslt_lang%22+value%3D%22en%22%2F%3E%0A%3Cparam+name%3D%22ext_xslt_hiddenattrs%22+value%3D%22%2Cip%2Civ%2C%22%2F%3E%0A%3Cparam+name%3D%22ext_xslt_tableId%22+value%3D%22historicalTable%22%2F%3E%0A%3Cparam+name%3D%22app%22+value%3D%22%2Findexes%2Fhistorical_prices%22%2F%3E%0A%3C%2Fpost%3E"

    With Http
        .Open "POST", postUrl, False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send sPostData
        Html.body.innerHTML = .responseText
    End With

    For Each elem In Html.getElementById("historicalTable").Rows
        For Each trow In elem.Cells
            C = C + 1: Cells(R + 1, C) = trow.innerText
        Next trow
        C = 0: R = R + 1
    Next elem
End Sub

Make sure to add the following references before executing the above script:

Microsoft XML, v6.0
Microsoft HTML Object Library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM