Monthly Archives: September 2013

Lokaal HTML-bestand parseren zonder Internet Explorer

Je kunt de Microsoft HTML Object Library gebruiken om een ​​lokaal HTML-bestand te parseren zonder Internet Explorer te gebruiken. Wat betekent dat? Je kunt een lokaal HTML-bestand openen met een webbrowser om het weer te geven of een teksteditor (Kladblok) om de HTML-code te bekijken, aan te passen of om er specifieke data uit te halen. Dat wordt ook wel webscraping genoemd. Bijvoorbeeld prijzen ophalen.

Iedereen die wel eens webscraping heeft gedaan, deed dat door op de achtergrond Internet Explorer te starten om naar een webadres te navigeren. Zodra de pagina klaar is, begint het navigeren door de zogenaamde DOM. Met de term DOM (Document Object Model) HTML wordt bedoeld dat de HTML-code van een webpagina omgezet wordt in een boomstructuur van objecten met behulp van de ‘Microsoft HTML Object Library’ (MSHTML). Maar wat te doen als IE niet beschikbaar is?

Je hebt een lokaal bestand nodig om mee te werken en een verwijzing naar de ‘Microsoft HTML Object Library’ te bereiken via:
Alt+F11 | Tools | References
Alt+F11 | Extra | Verwijzingen

We noemen het lokaal bestand: The_Local_File.html. Dat staat al hard-coded in de code. Het wordt automatisch aangemaakt. Vervolgens navigeren we naar:
“https://www.theguardian.com/europe” en slaan de complete pagina op in The_Local_File.html. Tenslotte halen we alle koppen op en drukken die af in het immediate venster (Direct venster) in VBA IDE (te bereiken via Alt+F11)

Option Explicit
'* Tools -> References Microsoft HTML Object Library
'* MSDN - URLDownloadToFile function - https://msdn.microsoft.com/en-us/library/ms775123(v=vs.85).aspx
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
        (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, _
        ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Sub Test()

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim sLocalFilename As String
    'The local file. You don't need to create it. Its created on the fly.
    sLocalFilename = Environ$("TMP") & "\The_Local_File.html"
    
    Dim sURL As String
    'Navigate to
    sURL = "https://www.theguardian.com/europe"
    
    
    Dim bOk As Boolean
    bOk = (URLDownloadToFile(0, sURL, sLocalFilename, 0, 0) = 0)
    If bOk Then
        If fso.FileExists(sLocalFilename) Then
        
            '* Tools -> References Microsoft HTML Object Library
            Dim oHtml4 As MSHTML.IHTMLDocument4
            Set oHtml4 = New MSHTML.HTMLDocument
            
            Dim oHtml As MSHTML.HTMLDocument
            Set oHtml = Nothing
            
            '* IHTMLDocument4.createDocumentFromUrl
            '* MSDN - IHTMLDocument4 createDocumentFromUrl method
            '- https://msdn.microsoft.com/en-us/library/aa752523(v=vs.85).aspx
            Set oHtml = oHtml4.createDocumentFromUrl(sLocalFilename, "")
            
            '* need to wait a little whilst the document parses
            '* because it is multithreaded
            While oHtml.readyState <> "complete"
            '* do not comment this out it is required to break into the code if in infinite loop
                DoEvents
            Wend
            Debug.Assert oHtml.readyState = "complete"
            

            Dim sTest As String
            sTest = Left$(oHtml.body.outerHTML, 100)
            '* just testing we got a substantial block of text, feel free to delete
            Debug.Assert Len(Trim(sTest)) > 50
            
            '* this is where the page information goes
            Dim htmlAnswers As Object 'MSHTML.DispHTMLElementCollection
            Set htmlAnswers = oHtml.getElementsByClassName("show-underline")
    
            Dim lAnswerLoop As Long
            For lAnswerLoop = 0 To htmlAnswers.Length - 1
                Dim vAnswerLoop
                Set vAnswerLoop = htmlAnswers.Item(lAnswerLoop)
                Debug.Print vAnswerLoop.outerText
            
            Next
    
        End If
    End If
End Sub