如何在“ id”下的html中获取内部文本

时间:2019-12-24 05:06:56

标签: excel vba web-scraping

我正在尝试这个。我对我的项目感到困惑,请帮助将这一特定点放在“ 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工作表中标记的值

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过不同的方式进行操作。原来,您在寻找错误的钥匙才能获得价值。此处的右键为totalTradedVolume

一种方法:

Sub GetPrice()
    Const URL = "https://www.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"":""(.*?)"""