I am using the following code to get a csv file from the link "https://www.nseindia.com/api/corporates-pit?index=equities&from_date=24-09-2023&to_date=24-12-2023&csv=true"
Note that "from_date=24-09-2023&to_date=24-12-2023" are dates. The difference between these two dates should be 4 months.
The downloaded csv file should then be renamed as "Insider.CSV"
from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.chrome.options import Optionsimport osimport timedef download_csv_with_selenium(): # Set up ChromeOptions to download files to a specific directory and include headers chrome_options = Options() download_directory = os.path.dirname(os.path.abspath("Insider.csv")) chrome_options.add_experimental_option("prefs", {"download.default_directory": download_directory}) chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36") chrome_options.add_argument("accept-language=en-US,en;q=0.9") chrome_options.add_argument("accept-encoding=gzip, deflate, br") chrome_options.add_argument("referer=https://www.nseindia.com/") # Initialize the WebDriver with Chrome and set options driver = webdriver.Chrome(options=chrome_options) try: # Open the URL driver.get("https://www.nseindia.com/companies-listing/corporate-filings-insider-trading") # Scroll down 300 pixels (adjust as needed) driver.execute_script("window.scrollBy(0, 300);") # Find and click the button with text "3M" button_3m = WebDriverWait(driver, 120).until( EC.element_to_be_clickable((By.XPATH, "//button[text()='3M']")) ) button_3m.click() # Wait for 10 seconds time.sleep(10) # Find and click the button with text "Download(.csv)" button_download = WebDriverWait(driver, 120).until( EC.element_to_be_clickable((By.XPATH, "//button[text()='Download(.csv)']")) ) button_download.click() # Wait for the download to complete (adjust this based on the expected download time) time.sleep(10) # Rename the downloaded file downloaded_file_path = os.path.join(download_directory, "DownloadedFile.csv") os.rename(downloaded_file_path, "Insider.csv") print("File downloaded and renamed successfully: Insider.csv") finally: # Close the browser window driver.quit()if __name__ == "__main__": # Download the CSV file with Selenium download_csv_with_selenium()
However when I run the script I am getting the following error on the webpage
The web page at https://www.nseindia.com/api/corporates-pit?index=equities&from_date=25-09-2023&to_date=25-12-2023&csv=true might be temporarily down or it may have moved permanently to a new web address.ERR_HTTP2_PROTOCOL_ERROR
I dont know how to resolve this