I need to read the closing price of a stock from the Yahoo Finance page. I had this answered before using Google Finance page, but the page is no longer available and I believe
Try the below way. It should fetch you the value of AAPL
from https://finance.yahoo.com/quote/
. To reach the value using class
name or tag
name is in reality troublesome. However, I used static id
here.
Sub Fetch_Quote()
Dim HTML As HTMLDocument, elem As Object, URL$
URL = "https://finance.yahoo.com/quote/AAPL?p=AAPL"
With CreateObject("InternetExplorer.Application")
.Visible = True
.navigate URL
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set HTML = .document
Set elem = HTML.getElementById("quote-market-notice").PreviousSibling.PreviousSibling
MsgBox elem.innerText
End With
End Sub
Then try this. Now, you should get the result with the blink of an eye:
Sub Fetch_Quote()
Dim HTML As New HTMLDocument, elem As Object, URL$
URL = "https://finance.yahoo.com/quote/AAPL?p=AAPL"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.send
HTML.body.innerHTML = .responseText
Set elem = HTML.getElementById("quote-market-notice").PreviousSibling.PreviousSibling
MsgBox elem.innerText
End With
End Sub
Reference to add to the library:
Microsoft XML, V6.0