【问题标题】:How to get data generated by script on HTML page using VBA?如何使用 VBA 在 HTML 页面上获取脚本生成的数据?
【发布时间】:2018-08-02 03:29:57
【问题描述】:

我正在尝试使用 VBA 解析 Website。我的目标是提取公司的数据(名称、网站、位置、投资者、投资日期、投资金额等)并将其存储在 Excel 表中。我提取 HTML 源代码的功能如下(至少在我的计算机上有效......)。我使用后期绑定以获得更好的可移植性。

Private Sub getHTMLFromURL()

    Dim objHTTP As Object
    Dim html As Object
    Dim implTmp As Object

    Set objHTTP = VBA.CreateObject("MSXML2.XMLHTTP")
    Set html = CreateObject("htmlfile")

    on error GoTo endProgram

    With objHTTP
        .Open "GET", URLStr, False
        .send

        If .READYSTATE = 4 And .Status = 200 Then
            html.Open
            html.write .responseText
            html.Close
        Else
            Debug.Print "Error" & vbNewLine & "Ready state: " & .READYSTATE & _
            vbNewLine & "HTTP request status: " & .Status
            GoTo endProgram
        End If

    End With

endProgram:
    Set html = Nothing
    Set objHTTP = Nothing
    If Err <> 0 Then
        Debug.Print "Error in getHTMLFromURL " & Err.Number & " - " & Err.Description
    End If

End Sub

我的问题是 HTML 是一个巨大的 SCRIPT 标记,其中隐藏了数据。例如,我猜公司的总部位置在这行代码中:

{"key":"hqLocations","name":"Location","sortable":false,"getText":"function getText(c) {\n    return getHQCity(c.hqLocations);\n  }"}

我谦虚地承认,我完全不知道如何获取这些数据。我在多个论坛中搜索了很多,但没有找到合适的答案。我试图适应this method,但没有成功。因此,我有几个与我的问题相关的问题:

  • 使用 MSXML2.XMLHTTP 是最好的方法吗?
  • 我是否必须对每个变量进行钓鱼,或者有没有办法直接解释脚本以获得包含所有数据的 HTML(这样会更容易)?
  • 否则,如何提取所有数据(单个数据和数组)?

非常感谢

【问题讨论】:

  • 您提供的描述只会模糊您希望获得的输出。请准确地说出您的预期输出。顺便说一句,不要提及类名或标签名:只需讲述该网站上可见的数据即可。
  • 我刚刚编辑了我的帖子,希望这样更清楚。我想或多或少地获得页面上显示的所有信息。尤其是“一般”框架和“融资轮次”框架中的内容。
  • 你已经注意到,使用XMLHTTP请求你会得到json响应。但是,当您想从该响应中解析所需字段时,您需要在.responsetextvba-json parser 上应用split 函数。最好的方法是选择InternetExplorer 作为选项。如果您对此没有问题,那么我可以尝试。谢谢。
  • 我很好奇你是怎么做到的。非常感谢您的帮助。

标签: javascript html vba parsing xmlhttprequest


【解决方案1】:

试试下面的脚本。当你运行它时,你应该得到你在帖子中请求的所需数据:

Sub Fetch_Data()
    Dim IE As New InternetExplorer, HTML As HTMLDocument
    Dim posts As Object, post As Object, hdata As Object
    Dim elem As Object, trow As Object

    With IE
        .Visible = False
        .navigate "http://app.startupeuropeclub.eu/companies/pld_space"
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set HTML = .document
    End With

    ''the following line is let the scraper wait until the data is completely loaded
    Do: Set hdata = HTML.getElementsByClassName("field"): DoEvents: Loop Until hdata.Length > 1

    For Each post In hdata
        With post.getElementsByClassName("title")
            If .Length Then R = R + 1: Cells(R, 1) = .Item(0).innerText
        End With
        With post.getElementsByClassName("description")
            If .Length Then Cells(R, 2) = .Item(0).innerText
        End With
    Next post

    ''avoiding hardcoded delay and wait until the data is completely loaded
    Do: Set posts = HTML.querySelector(".card-content.with-padding table.simple-table"): DoEvents: Loop While posts Is Nothing

    For Each elem In posts.Rows
        For Each trow In elem.Cells
            C = C + 1: Cells(I + 10, C) = trow.innerText
        Next trow
        C = 0
        I = I + 1
    Next elem
    IE.Quit
End Sub

添加到库的参考:

Microsoft Internet Controls
Microsoft HTML Object Library

【讨论】:

  • 哇,完美运行,非常感谢!! (我刚刚更改了Dim IE As Object, HTML As ObjectSet IE = VBA.CreateObject("InternetExplorer.Application") 以避免导入库。
猜你喜欢
  • 2021-11-13
  • 1970-01-01
  • 2019-01-14
  • 2013-05-15
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 2019-06-30
  • 2014-11-12
相关资源
最近更新 更多