This code’s goal is to create a pandas data frame from a data table that is scraped from a “https://www.investing.com/economic-calendar/” daily
the problem is that code is not working. i have tried scrap using by css_selector, class and ID
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pandas as pd
driver = webdriver.Chrome()
driver.get("https://www.investing.com/economic-calendar/")
# Scrape the data
events = []
delay = 10 # seconds
WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, '(/html/body/div[6]/section/div[6]/table')))
# Locate the rows in the table
rows = driver.find_elements(By.XPATH, '/html/body/div[6]/section/div[6]/table/tbody/tr[]')
for row in rows:
try:
event = row.find_element(By.XPATH, './td[4]').text
actual = row.find_element(By.XPATH, './td[5]').text
previous = row.find_element(By.XPATH, './td[7]').text
events.append([event, actual, previous])
except Exception as e:
print(f"Error processing row: {e}")
driver.quit()
df = pd.DataFrame(events, columns=['Event', 'Actual', 'Previous'])
df.head()
df.to_csv('economic_calendar.csv', index=False)
New contributor
Ismoiljon Jo’rayev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.