Excel 如何刮取一个类,如果没有找到刮另一个

Excel 如何刮取一个类,如果没有找到刮另一个,excel,vba,if-statement,do-while,Excel,Vba,If Statement,Do While,我正在使用VBA刮网站。我做的刮刀可以工作,但我想实现两个以上的功能,但我真的不知道如何做到这一点。代码如下: Sub pronutrition() Set ie = CreateObject("InternetExplorer.Application") my_url = "https://www.myprotein.ro/" ie.Visible = True i = 20 LastRow = ActiveSheet.Range("A" & ActiveSheet.Rows.Coun

我正在使用VBA刮网站。我做的刮刀可以工作,但我想实现两个以上的功能,但我真的不知道如何做到这一点。代码如下:

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\u fromValue”类,如果它没有找到它,则搜索“athenaProductBlock\u priceValue”,其次,如果它没有找到超过1或2个产品(范围设置为4),则停止搜索(现在,如果没有找到第二个或第三个产品,并且不会搜索下一个关键字,则返回并出错)

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

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

一旦你重新填充了集合,你就可以决定如何处理它。你可以循环并填充单个单元格,或者将结果合并以填充单个单元格。此外,如果
计数小于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
    TryExtractElementsByClassName = objCollection.Count > 0

End Function
要调用helper方法,请执行以下操作:

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

它停止的原因是因为您编写代码时假定所有这些元素都将存在。声明一个
元素
变量并捕获
getElementsByClassName
结果。然后在尝试
单击它或捕获它的文本之前检查是否找到任何元素。此外,您还没有提供实际的error您正在获取的以及在哪一行获取的?当它试图获取“athenaProductBlock\u priceValue”时,错误为“Object variable or With block variable not set”,但在页面上找不到此类,因为在某些页面上可以是“athenaProductBlock\u priceValue”,而在其他页面上可以是“athenaProductBlock\u fromValue”,这就是为什么我需要检查其中一个,如果它不存在,则检查另一个。尝试执行我建议的操作,希望能够解决您的问题哪一行突出显示?通过添加u解决了这一问题,但现在它显示对象IWebBrowser2的方法文档失败,并突出显示Set elements=ie.Document.getElementsByClassName(类名)