用VBA实现历史数据表的自动抓取
我正试图使用VBA从雅虎财经中获取历史数据表。但是,我只获得了1个数据,而不是整个表。有什么简单的方法吗?请帮帮我用VBA实现历史数据表的自动抓取,vba,Vba,我正试图使用VBA从雅虎财经中获取历史数据表。但是,我只获得了1个数据,而不是整个表。有什么简单的方法吗?请帮帮我 Public Sub History() Dim html As HTMLDocument, hTable As HTMLTable '<== Tools > References > Microsoft HTML Object Library Set html = New HTMLDocument With Cre
Public Sub History()
Dim html As HTMLDocument, hTable As HTMLTable '<== Tools > References > Microsoft HTML Object Library
Set html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://finance.yahoo.com/quote/0166.KL/history?p=0166.KL", False
.send
html.body.innerHTML = .responseText
End With
Application.ScreenUpdating = False
ActiveSheet.Cells(2, 2).Value = html.getElementsByClassName("Py(10px) Pstart(10px)")(0).innerText
Application.ScreenUpdating = True
End Sub
公共子历史记录()
将html设置为HTMLDocument,将HTTable设置为HTMLTable'引用>Microsoft html对象库
设置html=新的HTMLDocument
使用CreateObject(“MSXML2.XMLHTTP”)
.打开“获取”https://finance.yahoo.com/quote/0166.KL/history?p=0166.KL”“错
.发送
html.body.innerHTML=.responseText
以
Application.ScreenUpdating=False
ActiveSheet.Cells(2,2).Value=html.getElementsByClassName(“Py(10px)Pstart(10px)”)(0.innerText
Application.ScreenUpdating=True
端接头
您的代码只返回元素对象的第一个元素。这意味着…(0).innerText
请尝试下一个代码:
Public Sub History()
Dim html As HTMLDocument, hTable As HTMLTable '<== Tools > References > Microsoft HTML Object Library
Dim hist As Object, el As Variant, i As Long, j As Long, startCel As Range, boolDiv As Boolean
Set html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://finance.yahoo.com/quote/0166.KL/history?p=0166.KL", False
.send
html.Body.innerHTML = .responseText
End With
Application.ScreenUpdating = False
Set hist = html.getElementsByClassName("Py(10px) Pstart(10px)")
i = 2: j = 2
Set startCel = ActiveSheet.cells(1, 2)
Application.Calculation = xlCalculationManual
For Each el In hist
If j <= 7 Then
ActiveSheet.cells(i, j).Value = el.innerText: j = j + 1
Else
If InStr(el.innerText, "Dividend") > 0 Then boolDiv = True
j = 2: i = i + 1
ActiveSheet.cells(i, j).Value = el.innerText
If boolDiv Then
boolDiv = False: j = 2: i = i + 1
Else: j = j + 1: End If
End If
Next
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
公共子历史记录()
将html设置为HTMLDocument,将HTTable设置为HTMLTable'引用>Microsoft html对象库
Dim hist作为对象,el作为变量,i作为长,j作为长,startCel作为范围,boolDiv作为布尔值
设置html=新的HTMLDocument
使用CreateObject(“MSXML2.XMLHTTP”)
.打开“获取”https://finance.yahoo.com/quote/0166.KL/history?p=0166.KL”“错
.发送
html.Body.innerHTML=.responseText
以
Application.ScreenUpdating=False
Set hist=html.getElementsByClassName(“Py(10px)Pstart(10px)”)
i=2:j=2
设置startCel=ActiveSheet.cells(1,2)
Application.Calculation=xlCalculationManual
对于hist中的每个el
如果j 0,则boolDiv=True
j=2:i=i+1
ActiveSheet.cells(i,j).Value=el.innerText
如果布尔迪夫那么
boolDiv=False:j=2:i=i+1
否则:j=j+1:如果结束
如果结束
下一个
Application.Calculation=xlCalculationAutomatic
Application.ScreenUpdating=True
端接头
@Art76:您是否有时间测试上述代码?我已经测试了您的代码,但是它在1列中给出了数据。我修改了您的代码,以便将数据相应地分成7列40行,但它不起作用。Public Sub Reply()设置hist=html.getElementsByClassName(“Py(10px)Pstart(10px)”),对于j=1到7的ActiveSheet.Cells(i,j),i=1到40的hist中的每个El.Value=El.innerText Next Application.screenUpdate=True End Sub@Art76:我没有查看您试图提取内容的页面。我只注意到您处理对象的第一个元素,并尝试在对象元素之间进行迭代。我将尝试调整代码以安排页面中的废弃元素。FaneDuru-它按照我的计划工作得很好…非常感谢