我正在尝试从皇家造币厂网站获取硬币的价格。
我需要的元素没有 ID 或类名。
使用 Chrome 开发者工具,我想我已经在下面的代码中隔离了我需要的内容,但我不知道如何进一步进行。
Public Function XMLscrapeTest2() As Integer
Dim XMLpage As New MSXML2.XMLHTTP60
Dim HTMLdoc As New MSHTML.HTMLDocument
Dim HTMLelement As MSHTML.IHTMLElement
Dim HTMLelements As Object
Dim HTMLspan As HTMLSpanElement
Dim strURL As String
strURL = "https://www.royalmint.com/sovereign/all/1826-George-IV-Proof-Half-Sovereign/"
XMLpage.Open "GET", strURL, False
XMLpage.send
HTMLdoc.body.innerHTML = XMLpage.responseText
Set HTMLelements = HTMLdoc.getElementsByClassName("d-none d-md-block mb-0")
Debug.Print "d-none d-md-block mb-0>>>" & HTMLelements.length
Set HTMLelements = HTMLdoc.querySelectorAll("p")
Debug.Print HTMLelements.length
For intI = 0 To HTMLelements.length - 1
Debug.Print intI, HTMLelements(intI).getAttribute("classname")
Next intI
End Function
即时通讯输出
d-无 d-md-block mb-0>>>1
8
0
1
2
3
4
5
6 d-无 d-md-块 mb-0
7
您感兴趣的网页部分是通过 JavaScript 呈现的,因此您无法使用 HTML 解析器对其进行处理。但是,您可以将正则表达式应用于响应文本来获取价格。这是实现这一目标的方法之一:
Public Function XMLscrapeTest2() As Variant
Dim XMLpage As Object, Rxp As Object
Dim strURL As String, S As String
Dim oMatches As Object
Set XMLpage = CreateObject("MSXML2.XMLHTTP")
Set Rxp = CreateObject("VBScript.RegExp")
strURL = "https://www.royalmint.com/sovereign/all/1826-George-IV-Proof-Half-Sovereign/"
XMLpage.Open "GET", strURL, False
XMLpage.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
XMLpage.send
S = XMLpage.responseText
With Rxp
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "price"":""(.*?)"""
Set oMatches = .Execute(S)
If oMatches.Count > 0 Then
XMLscrapeTest2 = oMatches(0).SubMatches(0)
Else
XMLscrapeTest2 = "Not found"
End If
End With
End Function