您可以通过其类名 (button small download) 定位按钮:
IE.document.querySelector(".button.small.download").Click
或
IE.document.getElementsByClassName("button small download")(0).Click
但是:
如果您熟悉 JSON,则可以完全避免登录。您要点击的按钮上方有一个 JSON 链接:
data-data_url:
您可以将其与XMLHTTPRequest 一起使用来获取JSON 数据,然后使用JSONConverter. 等工具解析响应。将 .bas 添加到项目后,您需要转到 VBE>Tools>References 并添加对 Microsoft Scripting Runtime 的引用。
这里只是一个大纲,展示了设置初始 JSON 对象和提取一些信息的过程。
Option Explicit
Public Sub GetInfo()
Dim strURL As String, strJSON As String, Http As Object, json As Object
Application.ScreenUpdating = False
strURL = "https://housepriceindex.ca/_data/indx_data.json?d=4dfb05da"
Set Http = CreateObject("MSXML2.XMLHTTP")
With Http
.Open "GET", strURL, False
.send
strJSON = .responseText
End With
Set json = JsonConverter.ParseJson(strJSON)
Dim key As Variant, dictKeys As Variant
'****************************************
' Set json = json("data") ' Array("indx", "spc", "indx_ch", "spc_ch", "Meta", "Data") '<== These are the keys in that dict.
Set json = json("profiles") ' Array("c11", "mc","ab_calgary","ab_edmonton","bc_abbotsford","bc_kelowna" , _
"bc_vancouver","bc_victoria","mb_winnipeg","ns_halifax","on_barrie" , _
"on_brantford","on_guelph","on_hamilton","on_kingston","on_kitchener", _
"on_london","on_oshawa","on_ottawa","on_peterborough","on_st_catharines" , _
"on_sudbury","on_thunder_bay","on_toronto","on_windsor","qc_montreal","qc_quebec_city") '<==Keys in profile dict
Dim dict As Object, rowNumber As Long
Set dict = json("qc_montreal")
With ThisWorkbook.Worksheets("Sheet1")
For Each key In dict
rowNumber = rowNumber + 1
.Cells(rowNumber, 1) = key
.Cells(rowNumber, 2) = dict(key)
Next key
End With
Application.ScreenUpdating = True
End Sub
样品表输出:
示例源 JSON:
在顶级字典 ("JSON") 键 "data" 下的二级字典中有很多 C11 信息。