您可以使用 css 类选择器
Option Explicit
Public Sub GetInfo()
Dim html As HTMLDocument
Set html = New HTMLDocument '< VBE > Tools > References > Microsoft Scripting Runtime
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.justdial.com/Agartala/Abhay-Varieties-Nor-Banamalipu/9999PX381-X381-141028162716-U1Z5_BZDET", False
.send
html.body.innerHTML = .responseText
End With
Debug.Print html.querySelector(".ph_hdr").innerText
End Sub
如果你想要单独的行,那么使用
Dim items() As String, i As Long
items = Split(html.querySelector(".ph_hdr").innerText, ", ")
For i = LBound(items) To UBound(items)
If items(i) <> vbNullString Then
Activesheet.Cells(i + 1, 1) = items(i)
End If
Next
一个奇怪的情况是我通常会grab all the script tags with json using a css selector of script\[type='application/ld+json'\] and loop that looking for the info1。然而,尽管当我查看写出的文本文件时存在信息,但当我使用 DOM 解析器时,我找不到该信息。所以,尽管我很讨厌用 html 来提倡正则表达式,但这里有一个正则表达式解决方案:
Option Explicit
Public Sub GetInfo()
Dim html As HTMLDocument, s As String, re As Object
Set re = CreateObject("vbscript.regexp")
Set html = New HTMLDocument '< VBE > Tools > References > Microsoft Scripting Runtime
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.justdial.com/Agartala/Abhay-Varieties-Nor-Banamalipu/9999PX381-X381-141028162716-U1Z5_BZDET", False
.send
s = .responseText
html.body.innerHTML = s
Debug.Print html.querySelector(".fn").innerText
Debug.Print Trim$(Replace$(GetString(re, s, "title>(.*)<"), Chr$(34), vbNullString))
Debug.Print Trim$(Replace$(GetString(re, s, "streetAddress"":(.*"")"), Chr$(34), vbNullString))
Debug.Print Trim$(Replace$(GetString(re, s, "addressLocality"":(.*"")"), Chr$(34), vbNullString))
Debug.Print Trim$(Replace$(GetString(re, s, "postalCode"":(.*"")"), Chr$(34), vbNullString))
Debug.Print Trim$(Replace$(GetString(re, s, "addressRegion"":(.*"")"), Chr$(34), vbNullString))
Debug.Print Trim$(Replace$(GetString(re, s, "addressCountry"":(.*"")"), Chr$(34), vbNullString))
End With
End Sub
Public Function GetString(ByVal re As Object, ByVal inputString As String, ByVal pattern As String) As Variant
Dim matches As Object
With re
.Global = True
.MultiLine = True
.IgnoreCase = True
.pattern = pattern
If .test(inputString) Then
Set matches = .Execute(inputString)
GetString = matches(0).SubMatches(0)
Exit Function
End If
End With
GetString = "No match"
End Function
- 我会使用 Instr 寻找
"address":