Scraping data with vba from Yahoo finance

梦想的初衷 提交于 2021-02-05 12:20:32

问题


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 Google has completely changed its Finance page. I believe I can apply the same on Yahoo Finance with little modification.

Let s say Yahoo Finance has the following code for the stock symobol AAPL (Apple):

    ![YAHOO WEBPAGE CODE FOR AAPL][1]

I need to only extract the value 172.77.

This was working perfectly with Google Finance page. In my code below.

The line:

    "https://finance.google.com/finance?q="

is replaced by:

    "https://finance.yahoo.com/quote/"

The code loops in a range of cells and reads the stock symbols. We need to get the same results but from the Yahoo page instead.

    Sub ImportCurrentPriceNEW()
    Dim appIE As New InternetExplorer, html As HTMLDocument
    Dim item_data As Object    

    For k = 6 To 26 Step 1
     s = 1
     H = 1
     L = 1
     StopLoop = 0
     q = Format(k, "0")
     If IsEmpty(ActiveSheet.Range("$E$" & q).Value) = True Then  

     With appIE
    .Visible = False
    .navigate "https://finance.google.com/finance?q=" & Sheets("Up Trend 
     Stocks").Range("$A$" & q).Value
    Do Until .readyState = 4: DoEvents: Loop
    Set html = .document
    End With

   Set item_data = html.querySelector(".pr span")
   Range("$B$" & q).Value = item_data.innerText               
   End If    
Next    
appIE.Quit
Range("D1").Select

End Sub

Please let me know how I can modify code above to read data from Yahoo Finance page.


回答1:


Maybe you have an aversion to google, but I think you should consider using this small utility.

http://investexcel.net/multiple-stock-quote-downloader-for-excel/

That should do everything you want and a while lot more!!

Otherwise, in Excel, under the Data tab, click Existing Connections and then click MSN Money Central Investor Stock Quotes. See the image below. Enter your tickers/symbols, and click Open.

If the app is not installed already, click the link below, and follow the steps to get everything setup and working on your machine.

https://appsource.microsoft.com/en-us/product/office/WA104379220?tab=Overview




回答2:


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


来源:https://stackoverflow.com/questions/49510545/scraping-data-with-vba-from-yahoo-finance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!