2

I am using python 3.7 for this task.

Solutions tried:

  • I have tried using selenium but the data does not load on the webpage.
  • I tried using requests as well but there is no such URL which gives me proper data. The JSON data that can be fetched will give improper result because there is no way to set the expiry date.
  • And I cannot use already available libraries as they are fetching data from the old website.

Link to the option chain page on NSE website:

Harsh Mehta
  • 21
  • 1
  • 4
  • You should try fetching the base URL from the NSE site. Try Inspecting the page, there under Network tab you will be able to find the underlying URL. After looking into it. Here is the URL that you need --> https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY – Rahib Aug 29 '20 at 15:46
  • I have tried it but it gives an improper data as there is no way to set the expiry date. – Harsh Mehta Aug 31 '20 at 12:52
  • 1
    Try this code, You will have to add libraries and header section: `expiry_dt = '24-Sep-2020';` `url = 'https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY';` `data = requests.get(url, headers=header).content;` `data2 = data.decode('utf-8');` `df = json.loads(data2);` `json_ce = [data['CE'] for data in df['records']['data'] if "CE" in data and data['expiryDate'] == expiry_dt];` `df_ce = pd.json_normalize(json_ce);` – Rahib Sep 02 '20 at 05:08
  • 1
    this should be as answer. also i haven't seen python with semi colons ; ;) – Sushil Oct 06 '20 at 15:54
  • **Check this link for the solution** [link](https://stackoverflow.com/questions/65757973/how-do-i-get-the-live-option-chain-using-python/65804899#65804899) – Fred Jan 20 '21 at 07:48

0 Answers0