0

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")
1
  • The DataTables_Table_0 element does not exist on that page. What made you think it existed? Commented Feb 4, 2020 at 8:38

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the clear explanation with example. My mistake was not realizing the difference between DOM inspector and the actual HTML file. I believe your example emulates the dynamic generation of the content by waiting for it to load (?). Thanks again. I will also look into parsing JSON with 3rd party library as that seems like it would be very useful in the future with IE becoming MS Edge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.