core335
core335

Reputation: 1

Scraping web [VBA] xmlhttprequest

I am stuck with this. I am trying to learn how to use xmlhttprequest with VBA. My intention is to access the following url: "https://micuil.net/".

Once there, I can send values to the following fields, as seen in the image: image1

After pressing the button, the page displays the following information with the data I want to extract: image2 (return value)

I am able to complete what you see in image 1 by code, but I don't know how to get the result (image 2). Any help please?

    Function CuitEstimado2(sDni As Variant, sSexo As String) As String
    Set oDoc = New HTMLDocument
    Set oHTTP = New XMLHTTP60
    sSexo = IIf(sSexo = "f", "Mujeres", "Varones")
    sUrl = "https://micuil.net/"
    
    oHTTP.Open "GET", sUrl, False
    oHTTP.send
    sRespuesta = oHTTP.responseText
    oDoc.body.innerHTML = sRespuesta
    
    oDoc.getElementById("dni").Value = sDni
    oDoc.getElementsByName("sexo")(0).setAttribute("checked") = True
    oDoc.getElementById("btn").parentElement.Click
    End Function

Upvotes: 0

Views: 384

Answers (1)

Infrequent Coder
Infrequent Coder

Reputation: 99

Without having done heavy research for the specific website you are using, it may still be possible that the result you are looking for can be found within the response text (assuming I am understanding your dilemma properly).

First, it is recommended to perform a loop through the innerHTML of each element contained in the HTMLDoc. During your loop, use the InStr function to locate the result code as a string. It is a good idea to store each element that contains that result code into a collection for easy access after the loop.

It does get a bit more complicated from here, because the innerHTML of the corresponding elements may differ when pasting them into Notepad vs. trying to utilize in the VBE. However, if you can identify any unique JS (or other language) characters that will consistently indicate the location of the result code for each request, you may be able to use the Mid function to return the desired result into a string variable.

Upvotes: 1

Related Questions