Search code examples
excelvbaweb-scrapingxmlhttprequestfetch

unable to fetch data from web page - dom query


I used that code to automatically fetch some fields from the website and fill the excel table with search results but I think something changed on the website and I can not get this code working again...any help, suggestions?

Sub Scramble_NAVY_search()

Dim cel As Range, ms As Worksheet, dom As HTMLDocument
Set ms = Sheets("Scramble")
'Const searchUrl = "http://www.scramble.nl/index.php?option=com_mildb&view=search"

For Each cel In ms.Range("B2:B" & ms.Range("B" & Rows.Count).End(xlUp).Row).SpecialCells(2)
    Set dom = New HTMLDocument
    Application.ScreenUpdating = False
    With CreateObject("winhttp.winhttprequest.5.1")
        .Open "POST", searchUrl, False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send "Itemid=60&af=usn&serial=" & cel & "&sbm=Search&code=&searchtype=&unit=&cn="
        dom.body.innerHTML = .responseText
    End With

    On Error Resume Next
    With cel
        If .Offset(, -1).Value = "" Then
            .Offset(, 2) = dom.getElementsByClassName("rowBord")(0).Cells(1).innerText 'Code
            .Offset(, -1) = dom.getElementsByClassName("rowBord")(0).Cells(2).innerText 'Type
            .Offset(, 10) = dom.getElementsByClassName("rowBord")(0).Cells(3).innerText 'C/N
            .Offset(, 3) = dom.getElementsByClassName("rowBord")(0).Cells(4).innerText 'Unit
            .Offset(, 11) = dom.getElementsByClassName("rowBord")(0).Cells(5).innerText 'Status
        End If
    End With
   Next

    End Sub

Solution

  • Firstly, the line where your searchUrl is being declared, has been commented out. I am not sure if this was done knowingly and on purpose. So start by deleting ' in front of Const searchUrl.

    Secondly, change your searchUrl to this (basically change http to https):

    https://www.scramble.nl/index.php?option=com_mildb&view=search

    Finally, I have edited the code formatting in your post. Accept the edits and use that formatting. There were some line breaks there that would give an error.

    You should be ready to go.