0

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?

7
  • Ok, what's wrong here is that your URL, i.e. "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY" results in a json directly 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 with id="rawdata-tab" and clicks it but there is not html or webpage here. So, basically there is no point of using selenium here. Commented Nov 11, 2021 at 17:17
  • forget json and just f.write(data) Commented Nov 11, 2021 at 17:19
  • json.dump is for writing a file, json.dumps just returns the string. Commented Nov 11, 2021 at 17:24
  • Can you share the browser_base url? Commented Nov 11, 2021 at 17:38
  • 1
    @diggusbickus i took your suggestion and modified the code, able to save data in text file with open('data.txt', 'w') as f: f.write(data) time.sleep(5) driver.close() Commented Nov 11, 2021 at 17:54

2 Answers 2

1

As, I have mentioned in my comment, selenium is used for web scraping or more generally mimicking human actions on webpages. But your URL doesn't results in a webpage. Now, from what I understand, try this instead:

import json, requests

url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
 
response = requests.get(url)

d = response.json()

with open('data.json', 'w') as f:
    json.dumps(d, default=lambda o: '<not serializable>')

As your url already results in json, here I am just using requests to get it and it results in a valid json which you can then write however you want to a file.

PS: Try to understand whatever you're writing in code as this will help you debug, otherwise you are always going to get stuck somewhere.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your input. I understood the above code and tried it. But it does nothing no errors nothing
0

just add a user-agent to your request headers

import requests
headers={'User-Agent':'Mozilla/5.0 (Android 4.2.1; Mobile; rv:32.0) Gecko/32.0 Firefox/32.0'}
url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
data = requests.get(url, headers=headers)
with open('data.json', 'w') as f:
    f.write(data.text)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.