vba解析html页面
Sub test()
'vba解析html页面(字符串)
Dim ohtml, ohttp, table, byid, byname, byclassname, selectorall, selector As Object, s As String
Set ohtml = CreateObject("htmlfile")
Set ohttp = CreateObject("msxml2.xmlhttp")
With ohttp
.Open "get", "https://www.boc.cn/sourcedb/whpj/index.html", False
.send
ohtml.body.innerhtml = .responsetext
s = .responsetext
End With
Set byid = ohtml.getElementById("pjname") '根据id获取元素
Set table = ohtml.getElementsByTagName("table") '根据标签名字获取元素
Set byname = ohtml.getElementsByName("pjname") '根据name属性的值获取元素
Set byclassname = ohtml.getElementsByClassName("pjrq") '根据类样式的名字获取元素
Set selectorall = ohtml.querySelectorAll(".pjrq") '根据选择器获取元素,返回值是一个元素对象(疑似会报错,使用for each可以循环出数组),可获取css中的多个后代document.querySelectorAll(‘.box li’);
Set selector = ohtml.querySelector(".pjrq") '根据选择器获取元素,返回值是一个伪数组,可获取css中的一个后代document.querySelector(‘.box .bx’);document.querySelector(‘.box ul’);
Debug.Print 11
'For Each i In selectorall'遍历
' ss = i.innertext
' Debug.Print ss
'Next
Set byid = Nothing
Set table = Nothing
Set byname = Nothing
Set byclassname = Nothing
Set selectorall = Nothing
Set selector = Nothing
End Sub
标签:vba,Set,元素,获取,html,Nothing,ohtml,页面
From: https://www.cnblogs.com/yiblue/p/16907794.html