internet-explorer – Macro VBA fetch information on website

Question:

I need to get information from a real estate website and bring it to excel. I made the macro below:

Sub zap()

Set ie = CreateObject("InternetExplorer.Application")

With ie
.Navigate "http://www.zap.com.br/imoveis/fipe-zap/"
.Visible = True
End With

Do While ie.Busy
Loop
Do While ie.readyState <> 4
Loop
Call aguarda

Dim CurrentWindow As HTMLWindowProxy: Set CurrentWindow = ie.Document.parentWindow

'SELECIONA O NUMERO DE DORMITORIOS:  (0)=Todos  (1)=1 dormitório  (2)=2 dormitórios  (3)=3 dormitórios  (4)=4 dormitórios
ie.Document.All.Item("ctl00$ContentPlaceHolder1$quartosFipe")(0).Checked = True

'SELECIONA O PERÍODO:  (0)=Ano corrente  (1)=Últimos 12 meses  (2) =Todo o período
ie.Document.All.Item("ctl00$ContentPlaceHolder1$")(2).Checked = True

'SELECIONA SE É VENDA OU ALUGUEL:  (0)=Venda  (1)=Aluguel
ie.Document.All.Item("ctl00$ContentPlaceHolder1$radTransacao")(0).Checked = True

'SELECIONA A CIDADE:  (0)=FipeZap Ampliado  (1)=FipeZap Composto  (2)=Belo Horizonte  (3)=Brasilia  (4)=Curitiba  (5)=Florianopolis  (6)=Fortaleza  (7)=Niteroi  (8)=Porto Alegre  (9)=Recife  (10)=Rio de Janeiro  (11)=Salvador  (12)=Santo Andre  (13)=São Bernardo do Campo  (14)=São Caetano do Sul  (15)=São Paulo  (16)=Vila Velha  (17)=Vitoria
ie.Document.All.Item("ctl00$ContentPlaceHolder1$ddlCidadeIndiceFipeZap")(10).Selected = True

'ATUALIZA O GRÁFICO
ie.Document.parentWindow.execScript ("javascript:AtualizaGraficoIndice()")

    For Each element In ie.Document.getElementsByTagName("circle")
    x = element.getAttribute("cx")
    y = element.getAttribute("cy")
    Workbooks("FipeZap.xlsm").Worksheets("Plan1").Range("A1000000").End(xlUp).Offset(1, 0).Value = x
    Workbooks("FipeZap.xlsm").Worksheets("Plan1").Range("B1000000").End(xlUp).Offset(1, 0).Value = y
Next



End Sub

Sub-Wait:

Sub aguarda()
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 1
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
End Sub

However, not all values ​​returned by the site match the values ​​inspected in the graph. Although column B matches the values, the values ​​in column A don't match any of them.

Does anyone know if it's a problem with the macro (I don't think so) or if I'm missing some adjustment that ZAP Imoveis is doing on the page before plotting the graph?

Edition

Guys, the code just opens the ie, selects the criteria in the chart and inserts it into excel.

Here's a debug image of the zap page and the macro's return.

One of the information is not brought faithfully. See the screen below, the underlined information is brought up correctly, while the circled information is wrong.

http://s28.postimg.org/pil3834dp/tela_ie.png

http://s8.postimg.org/p7oykz2ud/tela_excel.png

Answer:

I've already found the solution.

In fact, ZAP Imoveis works with SVG in the graphic section.

My macro collects the x coordinate information from Internet Explorer, however there is some difference in IE's SVG chart implementation for the Chrome version of the site (where I was inspecting elements).

In addition to this difference in SVG implementation across browsers, I changed the code in some sections as it was updating the graph values ​​before the graph had fully loaded. As this is an SVG with animation, when changing the options, the animation changed the cx and cy values ​​dynamically and these were being captured by the macro before reaching their final value.

The final macro follows. It is a great example of a crawler and can be easily leveraged in the future.

Big hug!

Sub zap()

Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate "http://www.zap.com.br/imoveis/fipe-zap/"
    End With


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

Dim CurrentWindow As HTMLWindowProxy: Set CurrentWindow = ie.Document.parentWindow

'SELECIONA O NUMERO DE DORMITORIOS:  (0)=Todos  (1)=1 dormitório  (2)=2 dormitórios  (3)=3 dormitórios  (4)=4 dormitórios
ie.Document.All.Item("ctl00$ContentPlaceHolder1$quartosFipe")(1).Checked = True

'SELECIONA O PERÍODO:  (0)=Ano corrente  (1)=Últimos 12 meses  (2) =Todo o período
ie.Document.All.Item("ctl00$ContentPlaceHolder1$")(0).Checked = True

'SELECIONA SE É VENDA OU ALUGUEL:  (0)=Venda  (1)=Aluguel
ie.Document.All.Item("ctl00$ContentPlaceHolder1$radTransacao")(0).Checked = True

'SELECIONA A CIDADE:  (0)=FipeZap Ampliado  (1)=FipeZap Composto  (2)=Belo Horizonte  (3)=Brasilia  (4)=Curitiba  (5)=Florianopolis  (6)=Fortaleza  (7)=Niteroi  (8)=Porto Alegre  (9)=Recife  (10)=Rio de Janeiro  (11)=Salvador  (12)=Santo Andre  (13)=São Bernardo do Campo  (14)=São Caetano do Sul  (15)=São Paulo  (16)=Vila Velha  (17)=Vitoria
ie.Document.All.Item("ctl00$ContentPlaceHolder1$ddlCidadeIndiceFipeZap")(10).Selected = True

'ATUALIZA O GRÁFICO
ie.Document.parentWindow.execScript ("javascript:AtualizaGraficoIndice()")

'AGUARDA A ATUALIZAÇÃO
Application.Wait (Now + TimeValue("00:00:06"))

For Each element In ie.Document.getElementsByTagName("circle")
    x = element.getAttribute("cx")
    y = element.getAttribute("cy")
    Workbooks("FipeZap.xlsm").Worksheets("Plan1").Range("A1000000").End(xlUp).Offset(1, 0).Value = x
    Workbooks("FipeZap.xlsm").Worksheets("Plan1").Range("B1000000").End(xlUp).Offset(1, 0).Value = y
Next

End Sub
Scroll to Top