使用WinHTTP以表格形式刮擦标签输入

时间:2018-10-09 12:31:16

标签: excel vba excel-vba web-scraping winhttp

我自己克服了使用WinHTTP的连接问题(这是响应文本方法的Debug.Print的错误)。

所以我必须从一个表单(超过20个)中获取很多价值,然后创建一个字符串并将其传递给http://exampletry.it/visualizzaelenco.do以便生成PDF文件。

这是表单代码的示例。

<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="">
......

那么,如何在不使用标记名的情况下获得名称和值呢?我正在使用WinHTTP,但不想使用IE或其他网络浏览器。 (我只能使用.click以及VBA和IE来做到这一点)

添加的代码

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

1 个答案:

答案 0 :(得分:0)

我承认您不清楚自己想做什么。假设您位于表单中输入标签元素的属性valuename的后面,则可以使用CSS选择器以name属性作为所有表单元素的目标,并读出与元素匹配的结果name和值属性值。此外,假设每个元素都具有名称和值属性(看起来似乎)。

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