1

I am trying to get data from NSE website using the below python code and i am getting

[<span class="bold" id="orderBookTradeTMC">-</span>] 

insted of

[<span class="bold" id="orderBookTradeTMC">266</span>]

the data 266 is not loading please advice

import requests
import bs4
url="https://www.nseindia.com/get-quotes/equity?symbol=ITC"
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) 
   Chrome/57.0.2987.133 Safari/537.36'}
response = requests.get(url, headers=headers)
soup=   bs4.BeautifulSoup(response.text,'lxml')
print(soup.find_all(id='orderBookTradeTMC'))
Benson
  • 27
  • 1
  • 7

2 Answers2

0

The data is loaded dynamically via JavaScript. You can use requests module to simulate it.

For example:

import json
import requests


url="https://www.nseindia.com/get-quotes/equity?symbol=ITC"
api_url = 'https://www.nseindia.com/api/quote-equity?symbol={symbol}&section=trade_info'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0',}

symbol = url.split('=')[-1]
data = requests.get(api_url.format(symbol=symbol), headers=headers).json()

# uncomment this to print all data:
# print(json.dumps(data, indent=4))

print('Total market capitalization:', data['marketDeptOrderBook']['tradeInfo']['totalMarketCap'])

Prints:

Total market capitalization: 24082488.32
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

Try using Xpath for finding. I am not getting access to the server otherwise i was wishing to check myself.