VBA_DOM定位元素
1 '本例为前期绑定熟悉方法属性 引用这2个项目'Microsoft Internet Controls','Microsoft HTML Object Library' 2 'getElementsByClassName 后期绑定不支持 3 4 Sub DOM_定位() 5 Const url As String = "https://www.taifex.com.tw/cht/3/futDailyMarketReport" '期貨每日交易行情查詢 6 Dim IE As New InternetExplorer 7 Dim Dom As New MSHTML.HTMLDocument 8 'Set IE = CreateObject("internetexplorer. application") 9 'Set Dom = CreateObject("htmlfile") 10 With IE 11 .Visible = True 12 .Navigate url 13 14 Do While .Busy Or .readyState <> READYSTATE_COMPLETE 15 DoEvents 16 Loop 17 18 Do While .document.readyState <> "complete" 19 DoEvents 20 Loop 21 22 Dom.body.innerHTML = .document.body.innerHTML 23 24 Debug.Print "IE前期绑定:" 25 Debug.Print .document.getElementById("myMarketCode").innerText 26 Debug.Print .document.getElementsByName("queryDate")(0).outerHTML 27 Debug.Print .document.getElementsByTagName("h2")(0).innerText 28 Debug.Print .document.getElementsByClassName("title")(0).innerText 29 30 Debug.Print vbLf 31 32 Debug.Print "DOM前期绑定:" 33 Debug.Print Dom.getElementById("myMarketCode").innerText 34 Debug.Print Dom.getElementsByName("queryDate")(0).outerHTML 35 Debug.Print Dom.getElementsByTagName("h2")(0).innerText 36 Debug.Print Dom.getElementsByClassName("title")(0).innerText 37 38 .Quit 39 End With 40 End Sub