需要帮助才能从网站上抓取一些数据。 第一步,我设法访问该网站并导入我的变量,但是:
1.i不知道如何按“转换货币”按钮 2.然后获得“转换后的金额”和“费率”转换为Excel。
任何帮助将不胜感激!
Sub Test()
Dim IE As InternetExplorer
Dim Amount As String
Dim Source As String
Dim Target As String
Dim Datestring As String
Amount = 10000
Source = "Euro"
Target = "UK pound sterling"
Datestring = "03-08-2018"
'Open Browser and download data
Set IE = New InternetExplorer
With IE
.Visible = True
.Navigate "http://sdw.ecb.europa.eu/curConverter.do?sourceAmount=" & _
Amount & _
"&sourceCurrency=" & _
Source & _
"&targetCurrency=" & _
Target & _
"&inputDate=" & _
Datestring & _
"&submitConvert.x=209&submitConvert.y=10"
submitConvert.Click
While .Busy Or .readyState < 4: DoEvents: Wend
End With
结束子
答案 0 :(得分:1)
在没有打开浏览器的情况下更快速地使用XHR。
Option Explicit
Public Sub GetRates()
Dim sResponse As String, i As Long, html As New HTMLDocument, clipboard As Object
Dim sourceAmount As String, sourceCurrency As String, targetCurrency As String, inputDate As String
sourceAmount = "10000"
sourceCurrency = "EUR"
targetCurrency = "GBP"
inputDate = "03-08-2018"
Dim url As String
url = "http://sdw.ecb.europa.eu//curConverter.do?sourceAmount=" & sourceAmount & "&sourceCurrency=" & sourceCurrency & _
"&targetCurrency=" & targetCurrency & "&inputDate=" & inputDate & "&submitConvert.x=52&submitConvert.y=8"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", url, False
.send
sResponse = StrConv(.responseBody, vbUnicode)
End With
sResponse = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))
With html
.body.innerHTML = sResponse
Set clipboard = New MSForms.DataObject
clipboard.SetText .querySelectorAll("table.tableopenpage").item(1).outerHTML
clipboard.PutInClipboard
ActiveSheet.Cells(1, 1).PasteSpecial
End With
End Sub
不太坚固,但如果您只想
换算金额:
.querySelectorAll("table.tableopenpage").item(1).getElementsbytagname("td")(7).innertext
和费率:
.querySelectorAll("table.tableopenpage").item(1).getElementsbytagname("td")(10).innertext
输出:
参考:
Microsoft Forms Object Library (or add a userform to your project)
Microsoft HTML Object Library
由于您在URL中使用查询字符串,因此数据已经存在。无需点击。
只需使用正确的货币缩写即可。
Option Explicit
Public Sub Test()
Dim IE As InternetExplorer, Amount As String, Source As String, Target As String
Dim Datestring As String, hTable As HTMLTable
Amount = 10000
Source = "EUR"
Target = "GBP"
Datestring = "03-08-2018"
Dim url As String
url = "http://sdw.ecb.europa.eu/curConverter.do?sourceAmount=" & _
Amount & _
"&sourceCurrency=" & _
Source & _
"&targetCurrency=" & _
Target & _
"&inputDate=" & _
Datestring & _
"&submitConvert.x=209&submitConvert.y=10"
Set IE = New InternetExplorer
With IE
.Visible = True
.navigate url
While .Busy Or .readyState < 4: DoEvents: Wend
Dim clipboard As Object
Set clipboard = New MSForms.DataObject
clipboard.SetText .document.getElementsByClassName("tableopenpage")(1).outerHTML
clipboard.PutInClipboard
ActiveSheet.Cells(1, 1).PasteSpecial
End With
End Sub
如果对点击的方式感兴趣:
1)货币使用正确的3个字母缩写。
2)您可以使用以下命令单击提交按钮:
.document.querySelector("input[name=submitConvert]").Click
它使用
的CSS选择器input[name=submitConvert]
这是
带有input
标签的元素,其属性为name
,其值为submitconvert
。
3)然后,您需要
While .Busy Or .readyState < 4: DoEvents: Wend
允许页面刷新。
4)然后,您可以使用以下方法获取结果表:
.document.querySelectorAll("table.tableopenpage").item(1)
这将收集带有标签table
和类tableopenpage
的所有元素。您需要其中的第二个,在基于0的索引系统上为1。
需要参考:
Microsoft Internet Controls
Microsoft HTML Object Library
Microsoft Forms Object Library
其他
:我发现一次性获取表格更为简单,但是您可以指定费率,例如,更具体地,使用CSS选择器:
a[target*=quickview]
请注意,Excel在输出时可能会将日期从dd / mm / yyyy切换为mm / dd / yyyy,因此您需要更正此错误,或者至少要意识到这一点。