I am trying to save json data from https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY. The data.json file is empty. Here is the code
import time, json
from selenium import webdriver
driver = webdriver.Firefox()
url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
driver.get(url)
time.sleep(5)
button = driver.find_element_by_id("rawdata-tab")
button.click()
data = driver.find_element_by_class_name("data").text
d = json.loads(data)
with open('data.json', 'w') as f:
json.dumps(d, default=lambda o: '<not serializable>')
time.sleep(10)
driver.close()
Can't find where I am going wrong, is there a better way to achieve this?
"https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"results in ajsondirectly as it is an API call. Now,button = driver.find_element_by_id("rawdata-tab")this is a selenium code that finds an element in html code withid="rawdata-tab"and clicks it but there is not html or webpage here. So, basically there is no point of using selenium here.f.write(data)json.dumpis for writing a file,json.dumpsjust returns the string.with open('data.txt', 'w') as f: f.write(data) time.sleep(5) driver.close()