Last active
December 9, 2020 12:23
-
-
Save otavio-s-s/2c46cc454ce19c06b18337188cea6a49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for currency in currencies: | |
while True: | |
try: | |
# Opening the connection and grabbing the page | |
my_url = f'https://br.investing.com/currencies/usd-{currency.lower()}-historical-data' | |
option = Options() | |
option.headless = False | |
driver = webdriver.Chrome(options=option) | |
driver.get(my_url) | |
driver.maximize_window() | |
# Clicking on the date button | |
date_button = WebDriverWait(driver, 20).until( | |
EC.element_to_be_clickable((By.XPATH, "/html/body/div[5]/section/div[8]/div[3]/div/div[2]/span"))) | |
date_button.click() | |
# Sending the start date | |
start_bar = WebDriverWait(driver, 20).until( | |
EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[1]/input[1]"))) | |
start_bar.clear() | |
start_bar.send_keys(start) | |
# Sending the end date | |
end_bar = WebDriverWait(driver, 20).until( | |
EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[1]/input[2]"))) | |
end_bar.clear() | |
end_bar.send_keys(end) | |
# Clicking on the apply button | |
apply = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[5]/a"))) | |
apply.click() | |
sleep(5) | |
# Getting the tables on the page and quiting | |
dataframes = pd.read_html(driver.page_source) | |
driver.quit() | |
print(f'{currency} scraped.') | |
break | |
except: | |
driver.quit() | |
print(f'Failed to scrape {currency}. Trying again in 30 seconds.') | |
sleep(30) | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment