Страницы: 1
RSS
переменная объекта или переменная блока не установлена -91
 
Добрый день, не могу понять в чем проблема - переменные объявлены верно, set установлен, поиск по форумам и интернету не дает результата:
Код
Option Explicit

Public Sub Get_info()
    Dim sResponse As String, i As Long, html As New HTMLDocument

    Application.ScreenUpdating = False
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "[URL=https://shopping.google.es/search?q=3145891125306]https://shopping.google.es/search?q=3145891125306"[/URL];, False
        .send
        sResponse = StrConv(.responseBody, vbUnicode)
    End With
    sResponse = Mid(sResponse, InStr(1, sResponse, "<!doctype "))
    Dim titles As Object, targetedInfo As Object, rowCounter As Long
    With html
        .body.innerHTML = sResponse
        Set titles = .querySelectorAll(".SpKMTe")
        Set targetedInfo = .querySelectorAll(".DX0ugf ApBhXe")
    End With
    With Worksheets("Sheet1")
      For i = 0 To Len(targetedInfo) - 1 ' здесь ОШИБКА
            If i Mod 14 = 0 Then
                rowCounter = rowCounter + 1
                .Cells(rowCounter, 1) = titles(rowCounter - 1).innerText
                .Cells(rowCounter, 2) = targetedInfo(i).innerText
                .Cells(rowCounter, 3) = targetedInfo(i + 7).innerText
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
мне кажется надо объект targetedInfo объявить вот так
Код
Dim targetedInfo As MSHTML.IHTMLDOMChildrenCollection

а потом добиться, чтобы его свойство Length было больше 0, а потом вот так
Код
        For i = 0 To targetedInfo.Length - 1
            If i Mod 14 = 0 Then
                rowCounter = rowCounter + 1
                .Cells(rowCounter, 1) = titles(rowCounter - 1).innerText
                .Cells(rowCounter, 2) = targetedInfo.Item(i).innerText
                .Cells(rowCounter, 3) = targetedInfo.Item(i + 8).innerText
            End If
        Next i

так как сейчас при таком селекторе Set targetedInfo = .querySelectorAll(".DX0ugf ApBhXe") свойство Length = 0
Посмотрите как это делается тут http://exceldevelopmentplatform.blogspot.com/2018/01/vba-webscraping-jquery-selectors.html
Изменено: New - 08.01.2022 23:33:40
Страницы: 1
Наверх