-1

Any one of three highlighted part are the value i want to print. I am trying below code

Sub JJ()

Dim IE As New SHDocVw.InternetExplorer
Dim hdoc As MSHTML.HTMLDocument
Dim ha As String

IE.Visible = True
IE.navigate "https://www.nseindia.com/get-quotes/equity?symbol=DIVISLAB"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop

Set hdoc = IE.document
ha = hdoc.getElementById("preOpenFp").innerText
Debug.Print ha
End Sub

But the output is nothing pls help.

AAdi
  • 15
  • 4

1 Answers1

0

The website you're trying to scrape offers a very convenient way to do it. All you need to do is send an HTTP request and get the corresponding JSON response which looks like so:

enter image description here

If you take a look at the network traffic in your browser's developer tools, you'll see the requests that are being sent to the server when the page is being loaded. Among these requests you'll find the following one:

enter image description here

To send this request and get the info you need, you have to do the following:

Option Explicit

Sub nse()
Dim req As New MSXML2.XMLHTTP60
Dim url As String
Dim json As Object
url = "https://www.nseindia.com/api/quote-equity?symbol=DIVISLAB"

With req
    .Open "GET", url, False
    .send
    Set json = JsonConverter.ParseJson(.responseText)
End With

Debug.Print json("preOpenMarket")("IEP")

End Sub

This will print the value of IEP to your immediate window (in this case 2390). You can modify the code to best fit your needs.

To parse a JSON string you will need to add this to your project. Follow the installation instructions in the link and you should be set to go.

You will also need to add the following references to your project (VBE>Tools>References):

Microsoft XML version 6.0
Microsoft Scripting Runtime 
Stavros Jon
  • 1,695
  • 2
  • 7
  • 17
  • Thank you Stavros Jon, its working. I want know basics of JSON & How it works please suggest me tips. – AAdi Jul 25 '20 at 04:07
  • you are awesome this helped me. Please guide me to see the the json response as you show in picture. – AAdi Jul 25 '20 at 05:13
  • @AAdi To view the structure of a json string you can use a tool like this: http://jsonviewer.stack.hu/ – Stavros Jon Jul 25 '20 at 10:54
  • Please any answer other then json – AAdi Jul 27 '20 at 08:58
  • Hi @Stavros Jon, One more little help please view this question, "https://stackoverflow.com/questions/63098534/print-specific-data-with-json-perse/63099234#63099234" its for same site i just want to print the highlighted part. – AAdi Jul 29 '20 at 17:01
  • Now its showing error like "Error persing json expecting "{" or "[" " How to solve it @Stavros – AAdi Oct 01 '20 at 06:41