Azrul Zulkifli
Azrul Zulkifli

Reputation: 13

VBA Web Scraping: Object turns out empty (getelementbyID)

I intend to extract the information from the website below (website is in Malay) containing information on tender awards. https://tender.selangor.my/results?type=tenders

My code is as below, but the 'tenders' object appears as Nothing causing me unable to proceed further. Hope you can share some guidance on what I am doing wrong.

Worksheets("Data").Cells.Clear

Dim xhr As Object
Dim html As New HTMLDocument
Dim tenders As Object, item As Object, item2 As Object
Dim tender As Object
Dim i As Integer

Set xhr = CreateObject("MSXML2.XMLHTTP")

With xhr
    .Open "GET", "https://tender.selangor.my/results?type=tenders", False
    .send

    If .readyState = 4 And .Status = 200 Then
        html.body.innerHTML = .responseText
    Else
        MsgBox "Error" & vbNewLine & "Ready state: " & .readyState & _
        vbNewLine & "HTTP request status: " & .Status
    End If

End With

Set tenders = html.getElementById("DataTables_Table_0")

Upvotes: 0

Views: 188

Answers (1)

MITHU
MITHU

Reputation: 154

The tabular content that you are interested in are generated dynamically, so you can either make use of Internet Explorer or issue a get http requests with appropriate parameters to parse the json content using third party library. As the first option is easy to go with, I've created an example using the same:

Sub GetInformation()
    Dim tenders As Object

    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://tender.selangor.my/results?type=tenders"
        While .Busy Or .readyState < 4: DoEvents: Wend
        Application.Wait Now + TimeValue("00:00:05")
        Set tenders = .document.getElementById("DataTables_Table_0")
        Debug.Print tenders.innerText
        .Quit
    End With
End Sub

Upvotes: 2

Related Questions