I have been trying to scrape and parse a website for some financial data so that I can add the data to an Excel spreadsheet using VBA. I have found several possible solutions, but I cannot seem to get them to fit my parameters. My problem is that I only need one variable (Average Target Price) from a table. I have not been able to figure out what I am doing wrong. I will also be using a similar VBA format to check several hundred companies at a time so if there is a more efficient way to code what I have please let me know.
Here is what I have so far:
Sub ImportAnalystEst()
Dim oHtml As HTMLDocument
Dim oElement As IHTMLElement
Set oHtml = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", "http://www.marketwatch.com/investing/stock/aapl/analystestimates", False
.send
oHtml.body.innerHTML = .responseText
End With
Dim wsTarget As Worksheet
Dim i As Integer
i = 1
Set wsTarget = ActiveWorkbook.Worksheets("Sheet1")
For Each oElement In oHtml.getElementsByClassName("snapshot")
wsTarget.Range("A" & i) = Split(oElement.Children(0).innerText, "<TD>")
i = i + 1
Next
End Sub
Here is the HTML I am trying to pull from. Can someone please give an example of how I could extract the average target price of 146.52?
<div class="analystEstimates">
<div class="block">
<h2>Snapshot</h2>
</div>
<table class="snapshot">
<tbody>
<tr>
<td class="first">Average Recommendation:</td>
<td class="recommendation">
Overweight
</td>
<td class="first column2">Average Target Price:</td>
<td>146.52</td>
</tr>
<tr>
<td class="first">Number of Ratings:</td>
<td>
innerTextand use a Regular Expression to get at the value you want?Split(oHtml.getElementsByClassName("snapshot").item(0).firstchild.firstchild.innerhtml,"TD")(7)would return:>146.52</which you could then clean up.