JSON values extraction from webpage using MSXML2.XMLHTTP

本秂侑毒 提交于 2020-01-03 06:34:11

问题


Currently i am able to extract values from webpage but facing issue json value extraction.

I am using following code for other values extraction.

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)

Above code is not extracting following values of this url

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


回答1:


There isn't anything I can see in your code that attempts to extract this.

You could use regex to specify the appropriate pattern to extract that string. Below, the string you are after is stored in r variable.

EDIT:

Given your edit to the required string you can change the regex to:

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

Try it here

Former answer:

Try regex 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

Locals window check:


Regex explanation:



来源:https://stackoverflow.com/questions/56037729/json-values-extraction-from-webpage-using-msxml2-xmlhttp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!