【问题标题】:Scrape tag input in a form using WinHTTP使用 WinHTTP 在表单中抓取标签输入
【发布时间】:2018-10-09 12:31:15
【问题描述】:

我已经使用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

【问题讨论】:

    标签: excel vba web-scraping winhttp


    【解决方案1】:

    我承认不清楚您要做什么。假设您在表单中输入标记元素中的属性 valuename 之后,您可以使用 CSS 选择器来定位具有 name 属性的所有表单元素并读出结果匹配元素 name 和 value 属性值.此外,假设每个元素都有 name 和 value 属性(看起来)。

    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
    

    【讨论】:

    • 非常感谢!我使用了您的代码,但在 GetHTMLFileContent 上收到错误(未定义子或函数...)我在原始帖子中添加了代码。
    • 那是因为它是我从文件中读取的辅助函数。您应该使用底部示例来填充您的 html。我会调整的。
    • graet...我在html中添加了一个“o”..我的错误...并删除了表单中的名称...所以我必须获取每个“名称”和“值”并创建一个字符串,例如 name1=value1&name2=value2&... 在 winhttp 发布请求中发送到“visualizzadettaglio.do”并生成 .pdf
    猜你喜欢
    • 2018-07-11
    • 2017-05-07
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多