Scraping data with vba from Yahoo finance

后端 未结 2 636
误落风尘
误落风尘 2021-01-29 10:52

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

2条回答
  •  旧时难觅i
    2021-01-29 11:04

    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
    

提交回复
热议问题