Using Selenium & Beautiful Soup to export NSE Data

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from collections import Counter

from bs4 import BeautifulSoup
import pandas as pd

import urllib
import html5lib
import time

driver = webdriver.Chrome(executable_path="C:\\Users\\Pankaj_kumar18\\PycharmProjects\\chromedriver.exe")
driver.maximize_window()
driver.get("https://www.nseindia.com/market-data/equity-derivatives-watch")
time.sleep(10)

actions = ActionChains(driver)
actions.send_keys(Keys.ENTER).perform()

equity_derivatives = driver.find_element_by_xpath('//*[@id="main_livewth_deri"]/a')
actions.move_to_element(equity_derivatives).perform()
equity_derivatives.click()

html = driver.page_source
soup = BeautifulSoup(html,'html.parser')
time.sleep(10)

Derivates_Data = soup.find('table',{'class':'tablesorter'})

table_rows = Derivates_Data.find_all('tr')

#Inserting data into Table using Pandas
Dr_Data = []

for tr in table_rows:
    td = tr.find_all('td')
    row = [tr.text for tr in td]
    Dr_Data.append(row)

# Creating the table using pandas
derivatives_table = pd.DataFrame(Derivates_Data, columns=["InstrumentType", "Underlying", "ExpiryDate", "OptionType",
                                                    "Strike Price", "PrevClose", "OpenPrice", "HighPrice", "LowPrice",
                                                    "LastPrice", "No. of Contracts Traded", "Turnover (lacs)",
                                                    "Premium turnover (lacs)", "Underlying Value"])

derivatives_table = derivatives_table.drop([0], axis=0)
derivatives_table.to_csv("Equity Derivates Watch Table from NSE.csv", index=0)
time.sleep(10)

Selenium: Passing User Personalized Arguments for Browser launch

# Set the path to the chromedriver executable
webdriver_service = Service(ChromeDriverManager().install())

# Set Chrome options to download files to a specific folder
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--profile.default_content_settings.popups")
chrome_options.add_experimental_option("prefs", {
    "download.default_directory": "<path_to_download_folder>",
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True
})

# Initialize the Chrome browser with the configured options
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)

By Pankaj

One thought on “Export Equity Derivatives Data”
  1. This web page is really a walk-by means of for all the data you needed about this and didn抰 know who to ask. Glimpse here, and also you抣l undoubtedly uncover it.

Leave a Reply

Your email address will not be published. Required fields are marked *