【发布时间】: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