如何抓取一个类,如果找不到,则抓取另一个

问题描述 投票:0回答:1

我正在使用VBA抓取网站。我制作的刮板可以用,但是我想再实现2个功能,但我真的不知道该怎么做。这是代码:

Sub pronutrition()
Set ie = CreateObject("InternetExplorer.Application")
my_url = "https://www.myprotein.ro/"
ie.Visible = True
i = 20
LastRow = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row

Set Rng = ActiveSheet.Range("A20:A" & LastRow)
For Each cell In Rng

ie.navigate my_url
    Do While ie.Busy
        DoEvents
    Loop
    Wait 1

ie.Document.getElementsByName("search")(0).Value = cell


ie.Document.getElementsByClassName("headerSearch_button")(0).Click
    Do While ie.Busy
        DoEvents
    Loop
    Wait 2
ActiveSheet.Range("B" & i) = ie.Document.getElementsByClassName("athenaProductBlock_productName")(0).innerText + ie.Document.getElementsByClassName("athenaProductBlock_fromValue")(0).innerText
    Do While ie.Busy
        DoEvents
    Loop
    Wait 2

ActiveSheet.Range("C" & i) = ie.Document.getElementsByClassName("athenaProductBlock_productName")(1).innerText + ie.Document.getElementsByClassName("athenaProductBlock_fromValue")(1).innerText
    Do While ie.Busy
        DoEvents
    Loop
    Wait 2

ActiveSheet.Range("D" & i) = ie.Document.getElementsByClassName("athenaProductBlock_productName")(2).innerText '+ ie.Document.getElementsByClassName("athenaProductBlock_priceValue")(2).innerText
    Do While ie.Busy
        DoEvents
    Loop
    Wait 2

ActiveSheet.Range("E" & i) = ie.Document.getElementsByClassName("athenaProductBlock_productName")(3).innerText '+ ie.Document.getElementsByClassName("athenaProductBlock_priceValue")(3).innerText

    Do While ie.Busy
        DoEvents
    Loop
    Wait 2

i = i + 1
Next cell
ie.Quit
MsgBox "Done"
End Sub

首先,我要搜索“ athenaProductBlock_fromValue”类,如果找不到它,则要搜索“ athenaProductBlock_priceValue”,第二,如果发现的产品不超过1个或2个(范围设置为4)停止搜索(现在,如果没有找到第二或第三产品,并且不会去搜索下一个关键字,它会返回并返回错误)。

任何建议将不胜感激。谢谢!

excel vba if-statement do-while
1个回答
0
投票

使用辅助方法提取由HTMLCollection方法返回的getElementsByClassName。然后,您可以检查该方法是否返回任何结果。

一旦您将收藏夹取回,将由您自己决定如何处理。您可以循环并填充单个单元格,也可以将结果合并以填充单个单元格。另外,如果Count小于2,则忽略它,等等。

Private Function TryExtractElementsByClassName(ByVal ie As Object, 
                                               ByVal className As String, 
                                               ByRef objCollection As VBA.Collection) As Boolean

    'if ie is null, return false
    If ie Is Nothing Then Exit Function

    'if elements (HTMLCollection) is null, return false
    Dim elements As Object
    Set elements = ie.Document.getElementsByClassName(className)
    If elements Is Nothing Then Exit Function

    'fill collection
    Dim element As Object, idx As Long
    For idx = 0 To elements.Length
        Set element = elements(idx)
        If Not element Is Nothing Then objCollection.Add element
    Next idx

    'return True
    TryExtractElementsByClassName = objCollection.Count > 0

End Function

调用助手方法:

Sub Test()

    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")

    Dim objColl As New VBA.Collection

    'search single class name
    If TryExtractElementsByClassName(ie, "athenaProductBlock_priceValue", objColl) Then
        'handle results stored in objColl
    End If

    'search multiple class names separated by a space
    If TryExtractElementsByClassName(ie, "athenaProductBlock_priceValue athenaProductBlock_fromValue", objColl) Then
        'handle results stored in objColl
    End If

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.