VBA循环遍历多个HTML表

时间:2017-07-28 20:08:01

标签: vba excel-vba web-scraping html-table excel

以下是我尝试仅返回价格值的链接:https://www.express-supp...

我有一个VBA脚本,它将所有 product-grid-details 表值返回到工作簿中,但是某些值进入了错误的列,不允许生成pivot pivot表。但是,如果我更改此代码以生成名为 price-box 的表,则它根本不会返回任何值。

我认为页面上的HTML表格是乱序的,并且没有相互排序,这就是它使数据不在列中。作为一个解决方案,我希望VBA只返回页面的项目名称和价格,而不是全部。我该怎么做?

如果选择返回product-grid-details,如何将表返回到工作簿中的示例: n/a

以下是代码:

     With CreateObject("WINHTTP.WinHTTPRequest.5.1")
    .Open "GET", "https://www.express-supplements.co.uk/catalogsearch/result?q=Optimum+Nutrition", False
    .send
    oHtml.body.innerHTML = .responseText
    Debug.Print
 End With

 ReDim a(1 To 100000, 1 To 60)
 For Each oElement In oHtml.getElementsByClassName("product-grid-details")
    i = i + 1
    x = Split(oElement.innerText, vbCr)

    For ii = 1 To UBound(x)
        a(i, 1) = nowDate
        a(i, 2) = nowTime
        a(i, 3) = weblinks(webX, 1)
        a(i, 4) = weblinks(webX, 2)
        a(i, ii + 4) = Trim$(x(ii))
    Next

 Next oElement

    With SHwebdata
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Cells(LastRow + 1, 1).Resize(i, UBound(a, 2)) = a
        i = 0
    End With

1 个答案:

答案 0 :(得分:2)

你走了。只需运行它并按照您的要求获得结果:

Sub Web_Data()
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim topic As HTMLHtmlElement

    With http
        .Open "GET", "https://www.express-supplements.co.uk/catalogsearch/result?q=Optimum%20Nutrition", False
        .send
        html.body.innerHTML = .responseText
    End With

    For Each topic In html.getElementsByClassName("product-grid-details")
        With topic.getElementsByClassName("product-name")
            If .Length Then x = x + 1: Cells(x, 1) = .item(0).innerText
        End With
        With topic.getElementsByClassName("price")
            If .Length Then Cells(x, 2) = .item(0).innerText
        End With
    Next topic
End Sub
相关问题