VBA pick up information on website

Asked

Viewed 32 times

1

Good afternoon!

I have a macro to bring information from google shopping, but I can only bring the first value you find and I would like you to bring the 3 first ones you find.

Below is the test I was doing, however I say that I am layman and I am learning, who can help me I thank.

[![Google][1]][1]

Sub TesteBusca()

Dim IE, objElementCol, objElement, ClasseValores, ItemValores As Object
Dim i As Long

Set IE = CreateObject("InternetExplorer.Application")

With IE

   .navigate "https://www.google.com/shopping"
   .Visible = True

End With

Do Until (IE.readyState = 4 And Not IE.Busy)
        DoEvents
Loop


IE.document.all("q").Value = Range("A1").Text

Set objElementCol = IE.document.getElementsByTagName("input")

For Each objElement In objElementCol
    If objElement.Value = "Pesquisa Google" Then
        objElement.Click
        Exit For
    End If
Next objElement


Do Until (IE.readyState = 4 And Not IE.Busy)
        DoEvents
Loop


' tentando fazer ele passar por cada classe de "Nr22bf", onde está os valores dos itens e adicionar os 3 primeiros em cada coluna
Set ClasseValores = IE.document.getElementsByClassName("Nr22bf")
i = 1
For Each ItemValores In ClasseValores
i = i + 1
Cells(1, i) = ItemValores.innerText
Next ItemValores


' esse consigo fazer pegar apenas o primeiro resultado
'Set valor = IE.document.getElementsByClassName("Nr22bf").Item(0)
'Cells(1, 2) = valor.innerText

End Sub


  [1]: https://i.stack.imgur.com/8EQ0b.jpg

1 answer

0

Do you speak Massao blz? I made a change to use the Children method. I changed the class as well. Follow:

Sub TesteBusca()

Dim IE, objElementCol, objElement, ClasseValores, ItemValores As Object
Dim i As Long

Set IE = CreateObject("InternetExplorer.Application")

With IE

   .navigate "https://www.google.com/shopping"
   .Visible = True

End With

Do Until (IE.readyState = 4 And Not IE.Busy)
        DoEvents
Loop


IE.document.all("q").Value = Range("A1").Text

Set objElementCol = IE.document.getElementsByTagName("input")

For Each objElement In objElementCol
    If objElement.Value = "Pesquisa Google" Then
        objElement.Click
        Exit For
    End If
Next objElement


Do Until (IE.readyState = 4 And Not IE.Busy)
        DoEvents
Loop


' tentando fazer ele passar por cada classe de "Nr22bf", onde está os valores dos itens e adicionar os 3 primeiros em cada coluna
Set ClasseValores = IE.document.getElementsByClassName("sh-dlr__list-result")
i = 1
Dim filho As Object
For Each ItemValores In ClasseValores
    Set filho = ItemValores.Children(0).Children(1).Children(0).Children(1).Children(0).Children(0).Children(0)
i = i + 1
Cells(1, i) = filho.innerText 'ItemValores.innerText
Next ItemValores



' esse consigo fazer pegar apenas o primeiro resultado
'Set valor = IE.document.getElementsByClassName("Nr22bf").Item(0)
'Cells(1, 2) = valor.innerText

End Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.