excelvbaweb-scrapingprinting-web-page

Copy web-page content to a string


I need to access web-pages and copy their content (everything) into a string which I will then extract some figures from.

The web-page address changes each time, as I am basically accessing an online simulation tool and I have to specify the sim parameters each time. And the output is always a string of about 320 characters. the web page consists ONLY in that text.

Example of web address / query:

http://re.jrc.ec.europa.eu/pvgis5/PVcalc.php?lat=45&lon=8&peakpower=1&loss=14&optimalangles=1&outputformat=basic

Example of web-page content (string to retrieve): 37 0 1 54.9 72.1 7.21 2 73.1 96.0 12.0 3 114 149 15.5 4 121 160 17.9 5 140 185 11.3 6 142 188 9.31 7 161 212 10.2 8 149 197 10.0 9 123 162 10.3 10 83.0 109 15.5 11 55.8 73.3 13.5 12 55.8 73.2 9.47 Year 1270 1680 58.8 AOI loss: 2.7% Spectral effects: - Temperature and low irradiance loss: 8.0% Combined losses: 24.1%

Question to you

Is there a method to copy that string without having to open and close a browser each time? I have to repeat that operation (determine the query parameters, retrieve the relative string, extract the values that I need from the string) a total of 7200 times when I run my analysis and I'd like it to be as smooth and as fast as possible.

Note: I don't necessary need to save the string text on a document but it would be OK to do so if needed, and then open the file and retrieve my string. But that sounds so inefficient that I'm sure there must be a better way of doing it!


Solution

  • Yes there is a method to do this without using Internet Explorer, you can use a web request.

    Here is a sample method. Basically, you are emulating the communication that would normally take place between a browser and server.

    Option Explicit
    
    Public Function getPageText(url As String)
        With CreateObject("MSXML2.XMLHTTP")
            .Open "GET", url
            .send
            getPageText = .responseText
        End With
    End Function
    
    Sub Example()
        Dim url As String: url = "http://re.jrc.ec.europa.eu/pvgis5/PVcalc.php?lat=45&lon=8&peakpower=1&loss=14&optimalangles=1&outputformat=basic"
        Debug.Print getPageText(url)
    End Sub