Import Data in Excel from a table created by a script in a WebPage

前端 未结 2 1888
甜味超标
甜味超标 2020-12-11 23:54

I am trying to create a macro that automatically connect to a web page and import in excel the data from a table. My problem is that Excel Query tool does not recognize the

相关标签:
2条回答
  • 2020-12-12 00:20

    Try this

    Sub Dow_HistoricalData()
    
        Dim xmlHttp As Object
        Dim TR_col As Object, TR As Object
        Dim TD_col As Object, TD As Object
        Dim row As Long, col As Long
    
        Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0")
        xmlHttp.Open "GET", "http://www.investing.com/indices/us-30-historical-data", False
        xmlHttp.setRequestHeader "Content-Type", "text/xml"
        xmlHttp.send
    
        Dim html As Object
        Set html = CreateObject("htmlfile")
        html.body.innerHTML = xmlHttp.ResponseText
    
        Dim tbl As Object
        Set tbl = html.getElementById("curr_table")
    
        row = 1
        col = 1
    
        Set TR_col = html.getelementsbytagname("TR")
        For Each TR In TR_col
            Set TD_col = TR.getelementsbytagname("TD")
            For Each TD In TD_col
                Cells(row, col) = TD.innerText
                col = col + 1
            Next
            col = 1
            row = row + 1
        Next
    End Sub
    
    0 讨论(0)
  • 2020-12-12 00:32

    Another approach would be to make an HTTP request like

    // source http://tkang.blogspot.co.at/2010/09/sending-http-post-request-with-vba.html
    Dim result As String
    Dim myURL As String
    Dim winHttpReq As Object
    Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    myURL = "http://192.168.10.101:80/your_web_service?parameter=hello¶meter2=hi"
    
    winHttpReq.Open "GET", myURL, False
    winHttpReq.Send
    
    result = winHttpReq.responseText
    

    and parse the result. I haven't tried it myself though.

    0 讨论(0)
提交回复
热议问题