使用 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或其他Web浏览器。(我只能使用.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

我承认不清楚你想做什么。 假设您从表单中的输入标记元素中valuename属性,您可以使用CSS选择器定位具有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

最新更新