Macro VBA search information on site

Asked

Viewed 17,922 times

10

I need to search information on a real estate site and bring them to excel. I did 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 Espera:

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 chart. Although the B column hits the values, the A column values do not match any of them.

Can anyone tell if it’s a macro problem (I don’t think so) or if I’m missing some adjustment that ZAP Imoveis is doing on the page before plotting the chart?

Editing

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

Here is a screenshot of the debug of the Zap page and the return of the macro.

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

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

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

  • 3

    This is the most original Crawler I’ve ever seen. Congratulations!

  • Very interesting question. But, to make it easier, it would be nice for you to "explain" your code a little bit (not everyone will be able to interpret the code step-by-step). Well, I noticed that you force the execution of the function AtualizaGraficoIndice. I assume she’s the one updating the chart data, am I right? A half obvious question then: will the result of this execution is updated in the content (HTML) of your object ie in the VBA? I took a look at the documentation (http://msdn.microsoft.com/en-us/library/ie/ms536420(v=vs.85).aspx) and it says nothing about the content being updated...

  • Another thing, you even put some Debug.Print to display the values of x and y in its loop on the elements circle? (just to ensure that the problem is not formatting in Excel). By the way, if you include images in the question (screenshots) of the page values and Excel will facilitate in understanding the differences (and perhaps avoid silly questions like possibly this mine. hehehe).

  • Luiz, javascript updates the graph and displays the values of x and y in the HTML body within "Circle" elements. When inspected the valuables are there, correctly.

  • I’m glad you found the solution. But, dude, your question is still pretty messed up. In the IE image you added I did not find the id element highcharts-Series and much less elements with the tag circle. Moreover, the values you point to as right or wrong do not have an easy order to interpret if the reader does not perceive/understand the Range("A1000000").End(xlUp) in the code loop that you didn’t explain. Don’t take this comment lightly, my intention is to help make your question really interesting for other people besides yourself. :)

1 answer

5


already found the solution.

In fact, ZAP Immobles works with SVG in the graph section.

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

In addition to this difference in SVG implementation between browsers, I changed the code in some sections because it was updating the graph values before the graph had fully loaded. As it is an animation SVG, 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.

Follow the final macro. It is a great example of Crawler and can be easily used 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

Browser other questions tagged

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