首页 > 解决方案 > VBA 抓取 div 元素

问题描述

这只是一个测试,请忽略到目前为止你所看到的一切。

标签: testing

解决方案


你可以使用 xmlhttp,而不是浏览器,然后下面的循环写出所有的 div 信息。在如何仅获取感兴趣的数据时,我可能会更有选择性,但我希望以下内容符合您所要求的精神。

Option Explicit
Public Sub GetInfo()
    Dim data As Object, i As Long, html As HTMLDocument, r As Long, c As Long, item As Object, div As Object
    Set html = New HTMLDocument                  '<== VBE > Tools > References > Microsoft HTML Object Library
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.worten.pt/grandes-eletrodomesticos/maquinas-de-roupa/maquinas-de-roupa-ver-todos-marca-BALAY-e-BOSCH-e-SIEMENS?per_page=100", False
        .send
        html.body.innerHTML = .responseText
    End With
    Set data = html.getElementsByClassName("w-product__content")
    For Each item In data
        r = r + 1: c = 1
        For Each div In item.getElementsByTagName("div")
            With ThisWorkbook.Worksheets("Sheet1")
                .Cells(r, c) = div.innerText
            End With
            c = c + 1
        Next
    Next
End Sub

推荐阅读