[英]How to pull financial statements from Yahoo Finance (2019 URL) in VBA
I copied the code from this link Extract financial fundamental from Yahoo finance with excel我从这个链接复制了代码从雅虎金融用excel提取金融基础
The code from ASH works to pull balance sheet data;来自 ASH 的代码用于提取资产负债表数据; however, when I change the ticker (like MSFT), it begins pulling in the wrong data.
但是,当我更改股票代码(如 MSFT)时,它开始提取错误的数据。
How can I go about correcting it?我该如何去纠正它?
Sub Yahoo_BS() Dim xmlHttp As Object Dim TR_col As Object, Tr As Object Dim TD_col As Object, Td As Object Dim row As Long, col As Long Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0") myURL = "https://finance.yahoo.com/quote/SBUX/balance-sheet?p=SBUX" xmlHttp.Open "GET", myURL, False xmlHttp.setRequestHeader "Content-Type", "text/xml" xmlHttp.send Dim html As Object Set html = CreateObject("htmlfile") html.body.innerHTML = xmlHttp.responseText Dim tbl As Object Set tbl = html.getElementById("Pos(r)") row = 1 col = 1 Set TR_col = html.getElementsByTagName("TR") For Each Tr In TR_col Set TD_col = Tr.getElementsByTagName("TD") For Each Td In TD_col Cells(row, col) = Td.innerText col = col + 1 Next col = 1 row = row + 1 Next End Sub
the code works just fine for MSFT in so far as it works the same way for that ticker as it does for SBUX.该代码对 MSFT 工作得很好,因为它对那个股票的工作方式与对 SBUX 的工作方式相同。 The code you linked to is for retrieving balance sheet info for a given ticker.
您链接到的代码用于检索给定代码的资产负债表信息。
https://finance.yahoo.com/quote/SBUX/balance-sheet?p=SBUX
or或者
https://finance.yahoo.com/quote/MSFT/balance-sheet?p=MSFT
This does not guarantee you can 'lift and shift' this code for use with any of the other tabs eg income statement which has the following construction:这并不保证您可以“提升和移动”此代码以与任何其他选项卡一起使用,例如具有以下结构的损益表:
https://finance.yahoo.com/quote/MSFT/financials?p=MSFT
You will need to inspect the html of these tabs and see how it differs.您需要检查这些选项卡的 html 并查看它的不同之处。 There are already existing answers on StackOverflow covering how to obtain the data as shown in the other tabs (and by the different time periods eg Quarter).
StackOverflow 上已经有一些答案,涵盖了如何获取其他选项卡中显示的数据(以及不同的时间段,例如季度)。
VBA translation of existing answer .现有答案的VBA 翻译。 In VBA it would benefit from re-factoring:
在 VBA 中,它将受益于重构:
Option Explicit
Public Sub WriteOutFinancialInfo()
Dim http As Object, s As String
Set http = CreateObject("MSXML2.XMLHTTP")
With http
.Open "GET", "https://finance.yahoo.com/quote/MSFT/financials?p=MSFT", False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send
s = .responseText
End With
Dim html As MSHTML.HTMLDocument, html2 As MSHTML.HTMLDocument, re As Object, matches As Object
Set html = New MSHTML.HTMLDocument: Set html2 = New MSHTML.HTMLDocument
Set re = CreateObject("VBScript.RegExp")
html.body.innerHTML = s
Dim headers(), rows As Object
headers = Array("Breakdown", "TTM")
Set rows = html.querySelectorAll(".fi-row")
With re
.Global = True
.MultiLine = True
.Pattern = "\d{1,2}/\d{1,2}/\d{4}"
Set matches = .Execute(s)
End With
Dim results(), match As Object, r As Long, c As Long, startHeaderCount As Long
startHeaderCount = UBound(headers)
ReDim Preserve headers(0 To matches.Count + startHeaderCount)
c = 1
For Each match In matches
headers(startHeaderCount + c) = match
c = c + 1
Next
Dim row As Object
ReDim results(1 To rows.Length, 1 To UBound(headers) + 1)
For r = 0 To rows.Length - 1
html2.body.innerHTML = rows.Item(r).outerHTML
Set row = html2.querySelectorAll("[title],[data-test=fin-col]")
For c = 0 To row.Length - 1
results(r + 1, c + 1) = row.Item(c).innerText
Next c
Next
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws
.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End With
End Sub
Project references:项目参考:
VBE > Tools > References > Add reference to Microsoft HTML Object Library VBE > 工具 > 参考 > 添加对 Microsoft HTML 对象库的引用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.