2017-06-21 80 views
1

嗨我需要此代碼的幫助我試圖從此頁面提取數據https://finance.yahoo.com/quote/ADM.L/balance-sheet?p=ADM.L, 但問題是頁面默認設置爲年度,但我需要每季度的總資產值和總負債。使用Excel VBA提取雅虎財務數據

此代碼可以運行,但大部分時間都是選擇年度值。請提出一些我可以做的事情。

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 
+1

*請建議一些我可以做的事情。* - 在將元素寫入工作表之前,您可以使用IE選擇季度數據。 –

+0

我做到了,但總是無法正常工作......大多數情況下,它正在收集季度數據 –

+0

我認爲您希望它能夠提取季度數據? – Rdster

回答

0

這對你來說應該是一個好的開始。

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個不同的網址用於年度和季度。現在,顯然,他們給你2個鏈接點擊在財務報表的兩個頻率之間來回切換。

0

做下面的代碼修復。請注意,雅虎更新了類名稱,即從「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 
0

無法使用VBA從Yahoo提取季度數據。 年度數據可以提取,但不是季度。