ESun Bank Forex Historical Data Crawler as a Python script
#!/usr/bin/env python | |
############################################################################## | |
# fetchCurrencyData.py # | |
# # | |
# Requirements # | |
# # | |
# - Python 3 # | |
# - Selenium package for Python # | |
# - The web driver for Chrome # | |
# # | |
# 2018, Michael Chen; Apache 2.0 # | |
############################################################################## | |
import csv | |
import os | |
import sys | |
import time | |
from selenium import webdriver | |
currency = None | |
downloadPath = os.path.dirname(os.path.abspath(__file__)) | |
if len(sys.argv) < 2: | |
sys.stderr.write("No valid currency\n") | |
sys.exit(1) | |
currency = sys.argv[1] | |
# Create a new instance of the Chrome driver | |
driver = webdriver.Chrome() | |
# Go to ESun Bank Historical Data page | |
driver.get("https://www.esunbank.com.tw/bank/personal/deposit/rate/forex/exchange-rate-chart") | |
# Wait the page to fresh. | |
time.sleep(10) | |
# Select currency menu. | |
currencyArrow = driver.find_element_by_css_selector(".transformSelect li span") | |
currencyArrow.click() | |
# Select currency. | |
currencyOptions = driver.find_elements_by_css_selector(".transformSelectDropdown li span") | |
for c in currencyOptions: | |
if currency in c.text: | |
c.click() | |
break | |
# Wait the page to fresh. | |
time.sleep(3) | |
spotBtn = driver.find_element_by_css_selector(".radioBtns [for=\"spot\"]") | |
spotBtn.click() | |
# Wait the page to fresh. | |
time.sleep(3) | |
# Select duration. | |
durationBtn = driver.find_element_by_css_selector("div [for=\"oneYear\"]") | |
durationBtn.click() | |
# Wait the page to fresh. | |
time.sleep(3) | |
# Select data type. | |
dataBtn = driver.find_element_by_css_selector(".radioBtns [for=\"data\"]") | |
dataBtn.click() | |
# Wait the page to fresh. | |
time.sleep(3) | |
sys.stderr.write("Write data to csv file...\n") | |
with open("%sto%s.csv" % (currency, "TWD"), 'w', newline='') as csvfile: | |
csvwriter = csv.writer(csvfile) | |
csvwriter.writerow(["Date", "SellingRate", "BuyingRate"]) | |
hasMorePages = True | |
while hasMorePages: | |
items = driver.find_elements_by_css_selector("#inteTable tbody tr") | |
for item in items: | |
tds = item.find_elements_by_css_selector("td") | |
if tds[0].get_attribute("class") != "itemTtitle": | |
continue | |
csvwriter.writerow([tds[0].text, tds[1].text, tds[2].text]) | |
nextBtn = driver.find_element_by_css_selector(".pageNumberBlock .down") | |
if "active" in nextBtn.get_attribute("class"): | |
nextBtn.click() | |
else: | |
hasMorePages = False | |
time.sleep(1) | |
time.sleep(4) | |
# Close the browser. | |
driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment