【发布时间】:2021-07-01 21:11:17
【问题描述】:
需要一些帮助来从 forexfactory.com 抓取一个简单的表格。下面的代码可以工作,但格式很不稳定,而且那里的列似乎有一个不能正常工作的列。我知道我使用的代码很新手。有待改进。
Sub Pulldata2()
Dim ieObj As InternetExplorer
Dim appIE As Object
Dim htmlEle As IHTMLElement
Dim i As Integer
Dim strSheet As String
Dim LastRow As Long
strSheet = Sheet1.Range("A3")
Set ieObj = New InternetExplorer
ieObj.Visible = False
ieObj.navigate Sheet1.Range("A3").Value
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Wait Now + TimeValue("00:00:03")
Sheet2.Activate
For Each htmlEle In ieObj.document.getElementsByClassName("calendar__table")(0).getElementsByTagName("tr")
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
i = 1 + LastRow
With ActiveSheet
.Range("A" & i).Value = htmlEle.Children(1).textContent
.Range("B" & i).Value = htmlEle.Children(2).textContent
.Range("C" & i).Value = htmlEle.Children(3).textContent
.Range("D" & i).Value = htmlEle.Children(4).textContent
.Range("E" & i).Value = htmlEle.Children(5).textContent
.Range("F" & i).Value = htmlEle.Children(6).textContent
.Range("G" & i).Value = htmlEle.Children(7).textContent
.Range("H" & i).Value = htmlEle.Children(8).textContent
End With
i = i + 1
On Error Resume Next
Next htmlEle
End Sub
【问题讨论】:
-
请显示示例输出行的外观。例如,您想要实际值、预测值、先前值吗?了解输出应该是什么样子是良好抓取的第一步,这将有助于我们为您提供帮助。
-
嘿QHarr!谢谢回复。我更新了帖子以显示正在发生的事情。理想情况下,我想要输入的只是日期、货币、报告,然后是实际、预测和上一个。您可以在我的代码中看到我正在尝试跳过列。提前致谢 - 您之前曾帮助过我,非常感谢。
-
您只需要表格中的第一个(最新)日期吗?
-
是的 - 我不太挑剔。你说的是 TueApr 6 日期对吗?我只希望它在日期下作为单个单元格。
-
我的意思是,你只想要那个日期的数据,对吧?
标签: excel vba internet-explorer web-scraping