Search code examples
excelvbaweb-scrapinggetelementsbytagname

Web Scraping: innertext


I would like an overview of real estate prices for a specific region.
I created code which transfers the prices into a list and makes a statistic.
My program scrapes the price, area and so on from a specific link.

In the next step I would like to create automatically this link-list by a VBA.
For this step I need to to extract the exposeIDs to Excel.

Option Explicit

Sub ExposeID()
Dim browser As Object   'Aufnehmen der verwendeten Instanz des Browsers (Internet Explorer)
Dim knotenAst As Object 'Aufnehmen einer HTML Struktur aus dem Browser Dokument
Dim url As String       'Aufnehmen der auszulesenden Adresse
Dim ExposeID As String

url = "https://www.examplexyz.de"
Set browser = CreateObject("internetexplorer.application")
browser.Visible = False
browser.navigate url
Do Until browser.readyState = 4: DoEvents: Loop

Set knotenAst = browser.document.getElementsByClassName("is24-res-list is24-res-gallery result-list border-top")(0).getElementsBytagName("li")

'ExposeID
 If Not knotenAst Is Nothing Then
 ExposeID = Trim(knotenAst.innerText)
    
 Else
 ExposeID = "KeinWert"
 End If
 MsgBox ExposeID, vbOKCancel

'Aufräumen
browser.Quit
Set browser = Nothing
Set knotenAst = Nothing

End Sub

Expected Result: A list of all exposeIDs.

Actual Result: I get a Dump.

Screenshot: HTML from the homepage (tag is yellow highlighted)
enter image description here

Picture of my list:
[![Result in Excel][2]][2]


Solution

  • You should be able to use a combination of class and attribute css selectors

    Dim nodeList As Object, i As Long
    
    Set nodeList = browser.document.querySelectorAll(".result-list__listing[data-id]")
    
    For i = 0 To nodeList.Length - 1 
    
        Debug.Print nodeList.item(i).getAttribute("data-id")
    
    Next