【问题标题】:Extract data from yahoo finance using Excel VBA使用 Excel VBA 从 yahoo Finance 中提取数据
【发布时间】:2017-06-21 13:21:27
【问题描述】:

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

此代码会运行,但大多数时候它会选择年度值。请建议我能做什么。

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

【问题讨论】:

  • 请建议我能做些什么。 - 在将元素写入工作表之前,您可以使用 IE 选择季度数据。
  • 我做了,但它并不总是有效....主要是收集季度数据
  • 我以为你想让它获取季度数据?
  • 假设这一行:doc.getElementsByClassName("P(0px) M(0px) C($actionBlue) Bd(0px) O(n)")(2).Click 是点击季度数据链接的内容,将 VBA 代码暂停几秒钟以使事件赶上。我发现在使用 VBA 自动化 IE 时我必须经常这样做。
  • 我已经暂停了,请您自己运行一下看看这里是 url III.L、ABF.L、ADM.L 的代码

标签: vba excel extract yahoo-finance


【解决方案1】:

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

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 个链接,供您单击以在财务报表的 2 个频率之间来回切换。

【讨论】:

    【解决方案2】:

    请在下面找到代码修复。请注意,雅虎已经更新了类名,即从 "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
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2021-10-07
        • 2023-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多