简体   繁体   English

使用 Yahoo Finance 的 vba 抓取数据

[英]Scraping data with vba from Yahoo finance

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.我在使用 Google Finance 页面之前回答过这个问题,但该页面不再可用,我相信 Google 已经完全改变了它的 Finance 页面。 I believe I can apply the same on Yahoo Finance with little modification.我相信我可以在 Yahoo Finance 上应用相同的内容,只需稍作修改。

Let s say Yahoo Finance has the following code for the stock symobol AAPL (Apple):假设雅虎财经有以下股票符号 AAPL (Apple) 的代码:

    ![YAHOO WEBPAGE CODE FOR AAPL][1]

I need to only extract the value 172.77.我只需要提取值 172.77。

This was working perfectly with Google Finance page.这与 Google 财经页面完美配合。 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.我们需要从 Yahoo 页面获得相同的结果。

    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.请让我知道如何修改上面的代码以从雅虎财经页面读取数据。

Try the below way.试试下面的方法。 It should fetch you the value of AAPL from https://finance.yahoo.com/quote/ .它应该从https://finance.yahoo.com/quote/获取AAPL的价值。 To reach the value using class name or tag name is in reality troublesome.使用class名或tag名来达到这个值实际上很麻烦。 However, I used static id here.但是,我在这里使用了静态id

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

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/ 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.否则,在 Excel 中的“数据”选项卡下,单击“现有连接”,然后单击“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 https://appsource.microsoft.com/en-us/product/office/WA104379220?tab=Overview

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM