Not able to Import table (Option Chain) from link : https://www.nseindia.com/option-chain

Viewed 581

Was able to pull out data successfully but since the webpage is being modified I am finding it hard to pull out the table. The code that used to work initially with old link being : "https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=-10003&symbol=NIFTY&symbol=NIFTY&instrument=OPTIDX&date=-&segmentLink=17&segmentLink=17" was as follows ,

nse <- url1  %>% read_html() %>%
html_table(fill = TRUE)
nse_ticker <- nse[[3]] 

my_df <- as.data.frame(read_html(url1) %>% html_table(fill=TRUE))

Would be of great help if you can help me in pulling the data in html table format from the new link : https://www.nseindia.com/option-chain

1 Answers

The site calls API from:

  • https://www.nseindia.com/api/option-chain-indices?symbol=[index] if you choose a market index
  • https://www.nseindia.com/api/option-chain-equities?symbol=[equity] if you choose an equity symbol

You can use jsonlite::fromJSON() to read the data:

symbol <- "NIFTY"
is_market_index <- TRUE
if (is_market_index) {
  url <- paste0("https://www.nseindia.com/api/option-chain-indices?symbol=", symbol)
} else
  url <- paste0("https://www.nseindia.com/api/option-chain-equities?symbol=", symbol)
data <- jsonlite::fromJSON(url)
# The table you want is probably this one: 
do.call(data.frame, data$records$data)
Related