导入网络数据
Importing web data
我尝试使用 vba 和以下代码将网络数据导入 excel,但似乎没有 working.Hopefully 有人可以帮助解决这个问题,非常感谢!
Sub ExchangeRate()
Dim ieObj As InternetExplorer
Dim htmlEle As IHTMLElement
Dim i As Integer
i = 1
Set ieObj = New InternetExplorer
ieObj.Visible = True
ieObj.navigate "https://www.imf.org/external/np/fin/ert/GUI/Pages/Report.aspx?CU=%27SGD%27&EX=REP&P=OneWeek&CF=Compressed&CUF=Period&DS=Ascending&DT=Blank"
Application.Wait Now + TimeValue("00:00:05")
For Each htmlEle In ieObj.document.getElementsByClassName("default").Item(2).getElementsByClassName("row1").Item(4)
With ActiveSheet
.Range("A" & i).Value = htmlEle.Children(0).textContent
.Range("B" & i).Value = htmlEle.Children(1).textContent
.Range("C" & i).Value = htmlEle.Children(3).textContent
.Range("D" & i).Value = htmlEle.Children(4).textContent
End With
i = i + 1
Next htmlEle
End Sub
我认为您的问题是您选择要从中读取数据的元素所在的行。
Sub ExchangeRate()
Dim ieObj As InternetExplorer
Dim htmlEle As IHTMLElement
Dim htmlEleCollection As IHTMLElementCollection
Dim i As Integer
i = 1
Set ieObj = New InternetExplorer
ieObj.Visible = True
ieObj.navigate "https://www.imf.org/external/np/fin/ert/GUI/Pages/Report.aspx?CU=%27SGD%27&EX=REP&P=OneWeek&CF=Compressed&CUF=Period&DS=Ascending&DT=Blank"
' Application.Wait Now + TimeValue("00:00:05")
While ieObj.readyState <> 4 Or ieObj.Busy: DoEvents: Wend
' using application.wait may work, but if it takes more than 5s for the page to load your code may fail.
' waiting for the browser to be ready is better.
Set htmlEleCollection = ieObj.Document.getElementsByClassName("default").Item(0).getElementsByClassName("row1")
' i dont think you are selecting the right object with your code, i think this is what you were aiming for?
For Each htmlEle In htmlEleCollection
If htmlEle.Children.Length > 1 Then
' the table header has one child, the data rows have more
With ActiveSheet
.Range("A" & i).Value = htmlEle.Children(0).textContent
.Range("B" & i).Value = htmlEle.Children(1).textContent
' .Range("C" & i).Value = htmlEle.Children(3).textContent
' .Range("D" & i).Value = htmlEle.Children(4).textContent
' children 3 and 4 do not exist when i view the page so your code will fail with them in.
End With
End If
i = i + 1
Next htmlEle
ieObj.Quit
End Sub
我尝试使用 vba 和以下代码将网络数据导入 excel,但似乎没有 working.Hopefully 有人可以帮助解决这个问题,非常感谢!
Sub ExchangeRate()
Dim ieObj As InternetExplorer
Dim htmlEle As IHTMLElement
Dim i As Integer
i = 1
Set ieObj = New InternetExplorer
ieObj.Visible = True
ieObj.navigate "https://www.imf.org/external/np/fin/ert/GUI/Pages/Report.aspx?CU=%27SGD%27&EX=REP&P=OneWeek&CF=Compressed&CUF=Period&DS=Ascending&DT=Blank"
Application.Wait Now + TimeValue("00:00:05")
For Each htmlEle In ieObj.document.getElementsByClassName("default").Item(2).getElementsByClassName("row1").Item(4)
With ActiveSheet
.Range("A" & i).Value = htmlEle.Children(0).textContent
.Range("B" & i).Value = htmlEle.Children(1).textContent
.Range("C" & i).Value = htmlEle.Children(3).textContent
.Range("D" & i).Value = htmlEle.Children(4).textContent
End With
i = i + 1
Next htmlEle
End Sub
我认为您的问题是您选择要从中读取数据的元素所在的行。
Sub ExchangeRate()
Dim ieObj As InternetExplorer
Dim htmlEle As IHTMLElement
Dim htmlEleCollection As IHTMLElementCollection
Dim i As Integer
i = 1
Set ieObj = New InternetExplorer
ieObj.Visible = True
ieObj.navigate "https://www.imf.org/external/np/fin/ert/GUI/Pages/Report.aspx?CU=%27SGD%27&EX=REP&P=OneWeek&CF=Compressed&CUF=Period&DS=Ascending&DT=Blank"
' Application.Wait Now + TimeValue("00:00:05")
While ieObj.readyState <> 4 Or ieObj.Busy: DoEvents: Wend
' using application.wait may work, but if it takes more than 5s for the page to load your code may fail.
' waiting for the browser to be ready is better.
Set htmlEleCollection = ieObj.Document.getElementsByClassName("default").Item(0).getElementsByClassName("row1")
' i dont think you are selecting the right object with your code, i think this is what you were aiming for?
For Each htmlEle In htmlEleCollection
If htmlEle.Children.Length > 1 Then
' the table header has one child, the data rows have more
With ActiveSheet
.Range("A" & i).Value = htmlEle.Children(0).textContent
.Range("B" & i).Value = htmlEle.Children(1).textContent
' .Range("C" & i).Value = htmlEle.Children(3).textContent
' .Range("D" & i).Value = htmlEle.Children(4).textContent
' children 3 and 4 do not exist when i view the page so your code will fail with them in.
End With
End If
i = i + 1
Next htmlEle
ieObj.Quit
End Sub