【问题标题】:VBA webscraper - Return InnerHTML with regexVBA webscraper - 使用正则表达式返回 InnerHTML
【发布时间】:2018-05-26 02:57:38
【问题描述】:

使用 Excel VBA,我必须从 website 中抓取一些数据。

由于相关网站对象不包含id,我无法使用HTML.Document.GetElementById。

但是,我注意到相关信息始终存储在<div>-section 中,如下所示:

<div style="padding:7px 12px">Basler Versicherung AG &#214;zmen</div>

问题: 是否可以构造一个RegExp,它可能在循环中返回&lt;div style="padding:7px 12px"&gt; 和下一个&lt;/div&gt; 中的内容?

到目前为止我所拥有的是容器的完整InnerHtml,显然我需要添加一些代码来循环尚未构建的RegExp。

Private Function GetInnerHTML(url As String) As String
    Dim i As Long
    Dim Doc As Object
    Dim objElement As Object
    Dim objCollection As Object

On Error GoTo catch
   'Internet Explorer Object is already assigned
   With ie
        .Navigate url
        While .Busy
            DoEvents
        Wend
        GetInnerHTML = .document.getelementbyId("cphContent_sectionCoreProperties").innerHTML
    End With
    Exit Function
catch:
    GetInnerHTML = Err.Number & " " & Err.Description
End Function

【问题讨论】:

  • 除了标题,还有什么让你得出这个结论的?
  • 答案第一句你看过了吗?
  • 如果你展示一些预期输出的例子会有所帮助。你是在“Die Eingabe darf höchstens 255 Zeichen lang sein”之后吗?
  • @MartinDreher 看看here 和here。

标签: regex vba


【解决方案1】:

使用XMLHTTP 请求方法可以实现相同的另一种方法。试一试:

Sub Fetch_Data()
    Dim S$, I&

    With New XMLHTTP60
        .Open "GET", "https://www.uid.admin.ch/Detail.aspx?uid_id=CHE-105.805.649", False
        .send
        S = .responseText
    End With

    With New HTMLDocument
        .body.innerHTML = S
        With .querySelectorAll("#cphContent_sectionCoreProperties label[id^='cphContent_ct']")
            For I = 0 To .Length - 1
                Cells(I + 1, 1) = .Item(I).innerText
                Cells(I + 1, 2) = .Item(I).NextSibling.FirstChild.innerText
            Next I
        End With
    End With
End Sub

在执行上述脚本之前添加到库中的引用:

Microsoft HTML Object Library
Microsoft XML, V6.0

【讨论】:

  • 非常类似于 Ryan Wildry 的好主意!使用XMLHTTP 请求似乎可以大大提高性能。感谢您提供此额外信息!
【解决方案2】:

我认为您不需要正则表达式来查找页面上的内容。您可以使用元素的相对位置来找到您所追求的内容我相信。

代码

Option Explicit

Public Sub GetContent()
    Dim URL     As String: URL = "https://www.uid.admin.ch/Detail.aspx?uid_id=CHE-105.805.649"
    Dim IE      As Object: Set IE = CreateObject("InternetExplorer.Application")
    Dim Labels  As Object
    Dim Label   As Variant
    Dim Values  As Variant: ReDim Values(0 To 1, 0 To 5000)
    Dim i       As Long

    With IE
        .Navigate URL
        .Visible = False

        'Load the page
        Do Until IE.busy = False And IE.readystate = 4
            DoEvents
        Loop

        'Find all labels in the table
        Set Labels = IE.document.getElementByID("cphContent_pnlDetails").getElementsByTagName("label")

        'Iterate the labels, then find the divs relative to these
        For Each Label In Labels
            Values(0, i) = Label.InnerText
            Values(1, i) = Label.NextSibling.Children(0).InnerText
            i = i + 1
        Next

    End With

    'Dump the values to Excel
    ReDim Preserve Values(0 To 1, 0 To i - 1)
    ThisWorkbook.Sheets(1).Range("A1:B" & i) = WorksheetFunction.Transpose(Values)

    'Close IE
    IE.Quit
End Sub

【讨论】:

  • .NextSibling.Childen(0)... 真的就这么简单!非常感谢瑞恩!然而,出于性能原因,我倾向于使用 SIM 的 XMLHTTP 方法(IE 是我知道的唯一可能性)。
  • 保持它与您给定的代码一致,可以肯定的是,Web 请求方法是更好的方法。
  • 是的,我注意到您可能故意保留了我的大部分代码,这是逐步改进的好举措
猜你喜欢
  • 2017-01-18
  • 1970-01-01
  • 2012-02-27
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
相关资源
最近更新 更多