使用Excel VBA从雅虎财务中提取数据

您好我需要帮助这个代码我试图从这个页面提取数据https://finance.yahoo.com/quote/ADM.L/balance-sheet?p=ADM.L ,但问题是页是默认设置为年度,但我需要总资产和总负债的季度值。

这个代码运行,但大部分时间是select年度值。 请提出一些我可以做的事情。

Private Sub CommandButton3_Click() ' Dim ie As Object Set Rng = Range("A2:A50") Set Row = Range(Rng.Offset(1, 0), Rng.Offset(1, 0).End(xlDown)) Set ie = CreateObject("InternetExplorer.Application") With ie '.Visible = False For Each Row In Rng .navigate "https://finance.yahoo.com/quote/" & Range("A" & Row.Row).Value & "/balance-sheet?p=" & Range("A" & Row.Row).Value 'Application.Wait (Now + TimeValue("0:00:02")) While ie.readyState <> 4 Wend Do While ie.Busy: DoEvents: Loop Dim doc As HTMLDocument Set doc = ie.document doc.getElementsByClassName("P(0px) M(0px) C($actionBlue) Bd(0px) O(n)")(2).Click Do While ie.Busy: DoEvents: Loop Application.Wait (Now + TimeValue("0:00:05")) Range("D" & Row.Row).Value = doc.getElementsByClassName("Fw(b) Fz(s) Ta(end)")(4).innerText Range("E" & Row.Row).Value = doc.getElementsByClassName("Fw(b) Fz(s) Ta(end)")(12).innerText Range("F" & Row.Row).Value = doc.getElementsByClassName("C($gray) Ta(end)")(0).innerText Next Row End With ie.Quit ' End Sub 

这对你来说应该是一个好的开始。

 Sub DownloadData() Set ie = CreateObject("InternetExplorer.application") With ie .Visible = True .navigate "https://finance.yahoo.com/quote/ADM.L/balance-sheet?p=ADM.L" ' Wait for the page to fully load; you can't do anything if the page is not fully loaded Do While .Busy Or _ .readyState <> 4 DoEvents Loop Set e = ie.Document.GetElementsByClassName("Fz(s) Fw(500) D(ib) Pend(15px) H(18px) C($finDarkLink):h Mend(15px)")(1) e.Click ' Wait for the page to fully load; you can't do anything if the page is not fully loaded Do While .Busy Or _ .readyState <> 4 DoEvents Loop End With End Sub 

基本上,“年度”是默认值,您必须单击“季度”链接才能显示季度数据。 我相信雅虎曾经有2个不同的url为年度和季度。 现在,显然,他们给你2个链接点击在财务报表的两个频率之间来回切换。

在代码修复中find下面的代码。 请注意,雅虎已经更新了类名,即从“P(0px)M(0px)C($ actionBlue)Bd(0px)O(n)”==>“P(0px)M(0px)C($ c -fuji-blue-1-b)Bd(0px)O(n)“。

 Private Sub CommandButton3_Click() Dim ie As Object Set Rng = Range("A2:A50") Set row = Range(Rng.Offset(1, 0), Rng.Offset(1, 0).End(xlDown)) Set ie = CreateObject("InternetExplorer.Application") With ie .Visible = True For Each row In Rng .navigate "https://finance.yahoo.com/quote/" & Range("A" & row.row).Value & "/balance-sheet?p=" & Range("A" & row.row).Value While ie.readyState <> 4 Wend Do While ie.Busy: DoEvents: Loop Dim doc As HTMLDocument Set doc = ie.document Set element = doc.getElementsByClassName("P(0px) M(0px) C($c-fuji-blue-1-b) Bd(0px) O(n)")(2) element.Click Do While ie.Busy: DoEvents: Loop Range("D" & row.row).Value = doc.getElementsByClassName("Fw(b) Fz(s) Ta(end)")(4).innerText Range("E" & row.row).Value = doc.getElementsByClassName("Fw(b) Fz(s) Ta(end)")(12).innerText Range("F" & row.row).Value = doc.getElementsByClassName("C($gray) Ta(end)")(0).innerText Next row End With ie.Quit End Sub 

使用VBA从Yahoo提取季度数据是不可能的。 年度数据可以提取,但不是季度。