如何从带有 VBA 的 HTML 标签中提取值以在 Excel 中使用?

How to extract values from HTML tags with VBA to use in Excel?

代码段摘要:(第一个li-tag打开显示内容,其他li-tag相同,只是dd-tags中的值不同。

<body id=“WEBSITE“>
 <div> id="layout" class="  MAIN SECTION "</div>
  <main>
  <ul id=“RESULTS“>
  <li class="content" style="position:relative;">
  <dl>
   <dt class="first">HEAD01:</dt>
   <dd>VALUE01</dd>
   <dt class="first"> HEAD02:</dt>
   <dd> VALUE02</dd>
   <dt class="first"> HEAD03:</dt>
   <dd> VALUE03</dd>
   <dt class="first"> HEAD04:</dt>
   <dd> VALUE04</dd>
  </dl>
 </li>
<li class="content" style="position:relative;">… </li>
 <li class="content" style="position:relative;">… </li>
 <li class="content" style="position:relative;">… </li>
 <li class="content" style="position:relative;">… </li>
</ul>
</main>
</body>

li-tags 包含一个 object 的不同属性,每个属性都具有相同的 headers HEAD01、02、03 和 04(在“dt”),每个 li-tag 中的值都不同(在“dd”下)。我没有成功地提取标签中的 VALUE,使得它们在 Excel 中列为相应 header 下的列值,即 HEAD01 下所有 li.dd-tags 中的 Value01在 Excel table.

我的代码:

Public Sub GetData()

    Const url = "URL"
    Dim html As New HTMLDocument, Htmldoc As New HTMLDocument
    Dim RecsCnt As Object, x As Long
       
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False
        .send
        html.body.innerHTML = .responseText
    End With
                                   
     Set RecsCnt = html.querySelectorAll("li")
    'Set RecsCnt = html.querySelectorAll("dl")
    
    With ActiveSheet

       For x = 0 To RecsCnt.Length - 1
        .Cells(x + 2, 2) = html.querySelectorAll("dd").Item(0).innerText
       Next
        
    End With

End Sub

有没有人有有效的想法?谢谢

这应该会为您提供所需的数据 - 您只需要尝试格式化即可:

Sub Get_Text_from_website()

Dim IE As New InternetExplorer
With IE
    .Visible = True
    .navigate "https://versteigerungspool.de/amtsgericht/celle.92437/suche"
    While .Busy Or .readyState < 4: DoEvents: Wend
    Dim j As Long
    Dim element As Object, i As Long
    Set dtElements = IE.document.getElementsByTagName("dt")
    Set ddElements = IE.document.getElementsByTagName("dd")

    For Each element In dtElements
        ActiveSheet.Cells(i + 1, 1) = element.innerText
        i = i + 1
    Next

    For Each element In ddElements
        ActiveSheet.Cells(j + 1, 2) = element.innerText
        j = j + 1
    Next

    IE.Quit
End With

结束子

如果html如图所示,使用id和class会更快得到结果;您可以检索 headers 并链接 nextSibling 以移动到相邻元素。请注意:这是 set-up 到 运行 的 headers + 1 行。如果有更多请更新 html 以反映这一点。

Option Explicit

Public Sub GetData()

    Const URL = "URL"
    Dim html As MSHTML.HTMLDocument, x As Long
       
    Set html = New MSHTML.HTMLDocument
    
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .send
        html.body.innerHTML = .responseText
    End With

    With html.querySelectorAll("#RESULTS .first")
        
        For x = 0 To .Length - 1
            ActiveSheet.Cells(1, x + 1) = .Item(x).innerText
            ActiveSheet.Cells(2, x + 1) = .Item(x).NextSibling.NextSibling.innerText
        Next
        
    End With

End Sub