【问题标题】:Extracting data in json format using vba使用vba提取json格式的数据
【发布时间】:2021-12-13 15:31:27
【问题描述】:

我目前正在使用下面提到的代码从网络中提取数据,代码正在从网络中提取数据没有任何问题,现在我想以 json 格式提取相同的数据。我的代码在下面给出。我的页面 usrl 在下面给出 https://www.amazon.com/s?crid=AQO8JFH64H82&i=merchant-items&k=socks&me=A29M0OOY4LPNOT&qid=1635324852&ref=glow_cls&refresh=1&sprefix=socks%2Caps%2C315&low-price=0.1&high-price=5

Set Ie = CreateObject("InternetExplorer.Application")
Ie.Visible = True
Ie.navigate "https://www.amazon.com/s?crid=AQO8JFH64H82&i=merchant-items&k=socks&me=A29M0OOY4LPNOT&qid=1635324852&ref=glow_cls&refresh=1&sprefix=socks%2Caps%2C315&low-price=0.1&high-price=5"
Set html = Ie.Document
Set fso = CreateObject("Scripting.FileSystemObject")
Dim Fileout As Object
Set Fileout = fso.CreateTextFile("C:\Data\test" & Format(Now(), "_yyyy-mm-dd_hh-mm") & ".txt", True, True)
If html1 Like "*a-section a-spacing-small a-spacing-top-small*" Then
If html1 Like "*a-pagination*" Then
pagen = html.getElementsByClassName("a-section a-spacing-small a-spacing-top-small")(0).innerText
pagen = Replace(pagen, "'", "")
endpage = html.getElementsByClassName("a-pagination")(0).innerText
fileout.write endpage

【问题讨论】:

    标签: json vba extract


    【解决方案1】:

    首先,欢迎来到 SO。

    您必须决定是要自己解析 json,还是要使用几个可用库之一:

    1. 对于使用库,我建议先去Json解析器的THE SOURCE

    之后,请转至this answer;这是必须的,就这么简单:

    Dim p As Object
    Set p = JSON.parse(strFormattedJSON)
    

    但是,为了解析它,您需要了解内部结构,因为大多数 JSON 库将 JSON 对象解析为相互嵌套的数组或字典;也许你必须这样访问它们:

    Debug.Print p.Item("a-pagination")(0)
    
    1. 为了解析你自己,here is a very good implementationThis 也不错,领先to this one, the better

    PS:我们的搜索引擎非常好;)

    【讨论】:

    • 为什么你使用 p.Item(a-pagination")(0) 就像我在代码中使用类名一样?
    • @Satti22,在此解决方案中,将对象解析为 JSON 后,它将使用属性/元素的名称作为键进入数组或字典。我没有实现它,因为我在平板电脑中没有 VBA 编辑器;这就是为什么我写了也许你有
    • 仍然面临以下错误,通过在下一行引用参数类型不匹配。设置 p = JSON.parse(strFormattedJSON)。如果可能,请更新完整的解决方案。
    • @Satti22 我的回答只是提供线索并说明有两种基本方法。在接下来的 24 小时内,我将可以访问 VBA 编辑器,并尝试其中一种解决方案,并对其进行编辑
    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 2021-04-24
    相关资源
    最近更新 更多