POST website form data and retrieve results

后端 未结 1 1327
我在风中等你
我在风中等你 2020-12-20 10:21

I have been trying to write a VBA code to copy these three tables as shown in the web source code below. These tables show monthly weather data. Could someone please help me

相关标签:
1条回答
  • 2020-12-20 11:03

    It is unclear on what method you are using to access the web page but here is an XMLHTTP example that loops through all tables of a web page and returns the cell values to Sheet1.

    Dim htmlBDY As HTMLDocument, xmlHTTP As New MSXML2.ServerXMLHTTP60
    Dim iTD As Long, iTR As Long, iTBL As Long, eTR As MSHTML.IHTMLElement, ecTRs As IHTMLElementCollection
    xmlHTTP.Open "POST", "https://eosweb.larc.nasa.gov/cgi-bin/sse/grid.cgi", False
    xmlHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    'this post string uses the sample data on the form page. You can concatenate a string var to do the same thing
    xmlHTTP.send "email=skip@larc.nasa.gov&step=1&lat=33.5&lon=-80.75"
    
    Set htmlBDY = New HTMLDocument
    htmlBDY.body.innerHTML = xmlHTTP.responseText
    
    If CBool(htmlBDY.getElementsByTagName("table").Length) Then
        For iTBL = 0 To (htmlBDY.getElementsByTagName("table").Length - 1)
            Set ecTRs = htmlBDY.getElementsByTagName("table")(iTBL).getElementsByTagName("tr")
            For iTR = 0 To (ecTRs.Length - 1)
                If CBool(ecTRs(iTR).getElementsByTagName("th").Length) Then
                    For iTD = 0 To (ecTRs(iTR).getElementsByTagName("th").Length - 1)
                        Sheet1.Cells(iTR + 1, iTD + 1) = ecTRs(iTR).getElementsByTagName("th")(iTD).innerText
                    Next iTD
                ElseIf CBool(ecTRs(iTR).getElementsByTagName("td").Length) Then
                    For iTD = 0 To (ecTRs(iTR).getElementsByTagName("td").Length - 1)
                        Sheet1.Cells(iTR + 1, iTD + 1) = ecTRs(iTR).getElementsByTagName("td")(iTD).innerText
                    Next iTD
                End If
            Next iTR
            Set ecTRs = Nothing
        Next iTBL
    End If
    
    Set htmlBDY = Nothing
    Set xmlHTTP = Nothing
    

    I am using the XMLHTTP method to POST the form data into the CGI form. It is unlikely that you will be able to get to the page directly with a browser-based method. An InternetExplorer.Application method would likely have to go to the form, fill it in and submit it to get to the same page. Besides, the overhead (and time) of loading a browser object is huge compared to XMLHTTP.

        MSXML2.ServerXMLHTTP60 Form Submission

    You will need to go through Tools ► References and add Microsoft internet Controls, Microsoft HTML Object library and Microsoft XML 6.0 to your project.

    Note that I am checking for both <th> and <td> elements within the <tr>'s. There could conceivably be at least one more level of error control but I've never really seen any <table> that did not have at least one <tr>.

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