I have looked up multiple solutions but none works. If you open the URL in a normal browser, it will return data. I am unsure what is happening that it keeps loading if a timeout is not given when the python request is sent.
import urllib.request, json
with urllib.request.urlopen("https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY") as url:
data = json.loads(url.read().decode())
print(data)
I have tried a solution from here as well API request works in browser, but, doesn't work in postman or python requests module
import requests
url = "https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY"
payload = {}
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-IE,en-GB;q=0.9,en-US;q=0.8,en;q=0.7',
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)