如何更改XML编码

时间:2019-01-27 11:50:06

标签: excel vba

In the image the value under "open" inside red circle 749.10 i want in my excel sheet. 我正在做项目工作,而我需要此XML代码来快速工作。

我确实了解excel vba编码是我自己编写的,我已经尝试了很多,但是我无法以“ XML”方式完成相同的工作。

Sub JJ()

    Dim IE As New SHDocVw.InternetExplorer
    Dim hdoc As MSHTML.HTMLDocument
    Dim ha, hb, hc, hd As String


    IE.Visible = True
    IE.navigate "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=HAL"
    Do While IE.readyState <> READYSTATE_COMPLETE
    Loop

    Set hdoc = IE.document

    ha = hdoc.getElementById("open").innerText
    Range("K2").Value = ha

   IE.Quit

End Sub

我希望相同的工作可以使用XML代码

1 个答案:

答案 0 :(得分:0)

我想您是说您想转换为xmlhttp请求。一些内容是用javascript呈现的,但您想要的值也以json的形式出现在div中。

由于内容需要使用JavaScript进行填充,因此您可以在响应中看到定位的ID为空。

enter image description here

但是,div的其他位置也存在相同的信息:

您可以使用json解析器从该json解析出所需的值。我使用jsonconverter.bas。将.bas添加到您的项目后,您需要进行VBE > Tools > References > Add a reference to Microsoft Scripting Runtime。您还需要Microsoft HTML Object Library的参考。当您使用json viewer检查json响应时,将看到结构。 {}表示通过密钥访问的字典; []表示由索引访问的集合,带■的值是字符串文字。

Option Explicit
Public Sub GetInfo()
    Const URL As String = "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=HAL"
    Dim html As HTMLDocument, json As Object
    Set html = New HTMLDocument
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .Send
        html.body.innerHTML = .responsetext
    End With
    Set json = jsonconverter.ParseJson(html.getElementById("responseDiv").innerText)
    Debug.Print json("data")(1)("open")
End Sub

这是json的缩小视图:

enter image description here

相关问题