我正在试这个。我被困在我的项目中,请帮助将这一点放在"id“下。
Option Explicit
Public Sub GetlastPrice()
Dim ws As Worksheet, re As Object, p As String, r As String, URL As String
Set ws = ThisWorkbook.Worksheets("Sheet1")
p = """tradedVolume"":""(.*?)"""
Set re = CreateObject("VBScript.RegExp")
URL = "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=PEL"
With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", URL, False
        .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
        .send
        If .Status = 200 Then
            r = GetValue(re, .responseText, p)
        Else
            r = "Failed connection"
        End If
End With
ws.Range("B2").Value = r
End Sub
Public Function GetValue(ByVal re As Object, ByVal inputString As String, ByVal pattern As String) As String
With re
    .Global = True
    .pattern = pattern
    If .test(inputString) Then  ' returns True if the regex pattern can be matched agaist the provided string
        GetValue = .Execute(inputString)(0).submatches(0)
    Else
        GetValue = "Not found"
    End If
End With
End Function我希望在excel工作表中显示的标记值。

发布于 2019-12-24 14:57:18
你可以用不同的方式来做。事实证明,你找错了键来获取它的值。这里正确的关键是totalTradedVolume。
一种方法:
Sub GetPrice()
    Const URL = "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=PEL"
    Dim Html As New HTMLDocument, elem$, price$
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
        .send
        Html.body.innerHTML = .responseText
    End With
    elem = Html.querySelector("#responseDiv").innerText
    price = Split(Split(elem, "totalTradedVolume"":""")(1), """,")(0)
    MsgBox price
End Sub如果你坚持你已经尝试过的方法,在其中带来这个小的改变:
p = """totalTradedVolume"":""(.*?)"""https://stackoverflow.com/questions/59464067
复制相似问题