使用MSXML2.XMLHTTP从网页提取JSON值

时间:2019-05-08 09:41:03

标签: vba ms-access

目前,我能够从网页中提取值,但面临问题json值提取。

我正在使用以下代码提取其他值。

On Error Resume Next
Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", url1234, False
    http.Send
    html.body.innerHTML = http.ResponseText
    brand = html.body.innerText
    'MsgBox (brand)

以上代码未提取该网址的以下值

"" : {"0":"B0037RYT96","1":"B0152VYOQ2","2":"B0152WOT70","3":"B003W0NYKS","4":"B0152WOT8Y","5":"B00C2O7M1M","6":"B0037RMS6W","7":"B0037RMI0S","8":"B0152VYPXY"},

1 个答案:

答案 0 :(得分:0)

在您的代码中没有任何试图提取此内容的内容。

您可以使用正则表达式指定适当的模式来提取该字符串。在下面,您要搜索的字符串存储在r变量中。

编辑:

对所需的字符串进行编辑,可以将正则表达式更改为:

\"dimensionToAsinMap\" :(.*)[^\r\n].*

尝试here

以前的答案:

尝试正则表达式here

Option Explicit
Public Sub GetData()
    Dim s As String, r As String, re As Object
    Set re = CreateObject("vbscript.regexp")
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.yoursite.com?tag=stackoverfl08-20", False
        .send
        s = .responseText
    End With
    With re
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = "(""dimensionToAsinMap"" :.(.|\n)*)[;^\r\n].*return dataToReturn"
        If .test(s) Then
            r = .Execute(s)(0).SubMatches(0)
        Else
            r = "No match"
        End If
    End With
End Sub

本地窗口检查:


正则表达式说明: