excelvbaweb-scrapingwinhttp

Scrape tag input in a form using WinHTTP


I have overcome by myself connection problem using WinHTTP (it was an error with Debug.Print of the response text method).

So I have to take a lot of value from a form ( over 20) and then create a string and pass it to http://exampletry.it/visualizzaelenco.do in order to generate a PDF file.

It's a sample of the form code.

<BODY>
<form name="trattamentoForm" method="post" action="/ecportal/trattamento_dettaglio.do">
<input type="hidden" name="service" value="">
<input type="hidden" name="ufficioLoggato" value="">
<input type="hidden" name="uff_comp" value="DZT">
<input type="hidden" name="profiloUtente" value="U">
<input type="hidden" name="tipoModelloRicerca.codice" value="V">
<input type="hidden" name="tipoModelloRicerca.descrizioneEstesa" value="V - MODELLO V">
<input type="hidden" name="partRicerca" value="">
<input type="hidden" name="annoRicerca" value="">
<input type="hidden" name="codiceRicerca" value="123456789">
<input type="hidden" name="dataPresRicerca" value="">
<input type="hidden" name="numProtRicerca" value="">
<input type="hidden" name="concessionarioRicerca.codice" value="">
......

So how can I get the name and value without using tagname? I'm using WinHTTP and I don't want to use IE or other web browser. (I just able to do this using .click and VBA and IE)

ADDED CODE

oHtml.body.innerHTML = http.responseText
If http.Status = 200 Then




    Set OSTREAM = CreateObject("ADODB.Stream")
      OSTREAM.Open
     OSTREAM.Type = 1
      OSTREAM.Write http.responseBody
      File1 = "E:\test.html"
      OSTREAM.SaveToFile File1, 2
          OSTREAM.Close
      End If
        Dim html As HTMLDocument
        Set html = GetHTMLFileContent("E:\test.html")

        Dim list As Object, i As Long
        Set list = html.querySelectorAll("trattamentoForm")
        For i = 0 To list.length - 1
            Debug.Print "Name: " & list.Item(i).Name, "Value: " & list.Item(i).Value



        Next

Solution

  • I will admit to being unclear as to what you are trying to do. Assuming you are after the attributes value and name from the input tagged elements within the form you can use a CSS selector to target the all the form elements with a name attribute and read out the results matched elements name and value attribute values. Also, assumes each element has both name and value attributes (which appear to).

    Option Explicit
    Public Sub test()
        Dim html As HTMLDocument
        Set html = New HTMLDocument
        With CreateObject("WINHTTP.WinHTTPRequest.5.1")
            .Open "GET", "yourURL", False
            .send
            html.body.innerHTML = .responseText
        End With
    
        Dim list As Object, i As Long
        Set list = html.querySelectorAll("form input[name]")
        For i = 0 To list.Length - 1
            Debug.Print "Name: " & list.item(i).NAME, "Value: " & list.item(i).Value
        Next
    End Sub