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.
This is the most original Crawler I’ve ever seen. Congratulations!
– Leonel Sanches da Silva
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 objectie
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...– Luiz Vieira
Another thing, you even put some
Debug.Print
to display the values ofx
andy
in its loop on the elementscircle
? (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 Vieira
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.
– PunchTheNewbie
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 tagcircle
. 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 theRange("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. :)– Luiz Vieira