正如所评论的,发布的特定 URL 是一个 XML,它使用 XSLT 样式表将页面呈现为 HTML。但底层来源和响应文本是XML。Ctrl使用+查看 XML 数据源U:
XML
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="http://www.floatrates.com/currency-rates.xsl" ?>
<channel>
<title>XML Historical Foreign Exchange Rates for U.S. Dollar (USD) (4 Feb 2021)</title>
<link>http://www.floatrates.com/currency/usd/</link>
<xmlLink>http://www.floatrates.com/daily/usd.xml</xmlLink>
<description>XML historical foreign exchange rates for U.S. Dollar (USD) from the Float Rates. Published at 4 Feb 2021.</description>
<language>en</language>
<baseCurrency>USD</baseCurrency>
<pubDate>Thu, 4 Feb 2021</pubDate>
<lastBuildDate>Thu, 4 Feb 2021</lastBuildDate>
<item>
<title>1 USD = 0.832481 EUR</title>
<link>http://www.floatrates.com/usd/eur/</link>
<description>1 U.S. Dollar = 0.832481 Euro</description>
<pubDate></pubDate>
<baseCurrency>USD</baseCurrency>
<baseName>U.S. Dollar</baseName>
<targetCurrency>EUR</targetCurrency>
<targetName>Euro</targetName>
<exchangeRate>0.832481</exchangeRate>
<inverseRate>1.201229</inverseRate>
<inverseDescription>1 Euro = 1.201229 U.S. Dollar</inverseDescription>
</item>
<item>
<title>1 USD = 0.733621 GBP</title>
<link>http://www.floatrates.com/usd/gbp/</link>
<description>1 U.S. Dollar = 0.733621 U.K. Pound Sterling</description>
<pubDate></pubDate>
<baseCurrency>USD</baseCurrency>
<baseName>U.S. Dollar</baseName>
<targetCurrency>GBP</targetCurrency>
<targetName>U.K. Pound Sterling</targetName>
<exchangeRate>0.733621</exchangeRate>
<inverseRate>1.363101</inverseRate>
<inverseDescription>1 U.K. Pound Sterling = 1.363101 U.S. Dollar</inverseDescription>
</item>
...
</channel>
但是您仍然可以解析响应返回并在<item>
节点数据上运行 XPath。只需使用 MSXML 的DomDocument
withLoadXML
和 SelectNodes
方法。
VBA
Sub CallFunc()
Call GetHistoricFX("USD", "", "2021-02-04")
End Sub
Function GetHistoricFX(fromCurr As String, toCurr As String, AsofDate As Date) As String
On Error GoTo ErrHandle
Dim xmlHttp As Object
Dim sUrl As String
Dim xmldoc As Object, itemNodes As Object, itemNode As Variant, chNode As Variant
Dim i As Long, j As Long
' Create an XMLHTTP object
Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
' get the URL to open
sUrl = "http://www.floatrates.com/historical-exchange-rates.html?" _
& "currency_date=" & AsofDate _
& "&base_currency_code=" & fromCurr _
& "&format_type=xml"
' open connection and get data
xmlHttp.Open "GET", sUrl, False
xmlHttp.send
' CREATE A DOMDocument OBJECT FROM RESPONSE
Set xmldoc = CreateObject("MSXML2.DOMDocument")
xmldoc.LoadXML xmlHttp.responseText
xmldoc.setProperty "SelectionLanguage", "XPath"
Set itemNodes = xmldoc.SelectNodes("//item")
' ITERATE THROUGH ITEM NODES AND CHILDREN
With ThisWorkbook.Worksheets("MAIN")
i = 2
For Each itemNode In itemNodes
j = 1
For Each chNode In itemNode.SelectNodes("*")
If i = 2 Then
.Cells(i - 1, j) = chNode.tagName
End If
.Cells(i, j).Value = chNode.Text
j = j + 1
Next chNode
i = i + 1
Next itemNode
End With
MsgBox "Successfully completed!", vbInformation
ExitHandle:
Set chNode = Nothing
Set itemNode = Nothing
Set itemNodes = Nothing
Set xmldoc = Nothing
Set xmlHttp = Nothing
Exit Function
ErrHandle:
MsgBox Err.Number & " - " & Err.Description, vbCritical
Resume ExitHandle
End Function
输出
