Series Data Voyage Training Log

~1 min read

Collecting Exchange Rates Automatically Every Day

Collecting Hana Bank exchange rates with Selenium while defending against holidays and errors, then loading into a DB via an external dbio module. Crawling was completed by running into errors in practice.

TrainingScrapingSelenium

Code 1, Code 2, ... Success, Final

What struck me in this notebook were the cell titles. They read "Code 1", "collection error: HTML loaded wrong", "success", "final", and "fix", in that order. Crawling isn't finished in one shot. The process of hitting an error, finding the cause, and fixing it stayed in the record.

Setting the Path to Import a Module

Because the working file and the shared module (dbio) lived in different places, I learned to add a path to sys.path.

import sys, os
target_dir = os.path.abspath("../../")
if target_dir not in sys.path:
    sys.path.append(target_dir)
from dbio import to_db

Holiday-Handling Logic

Exchange rates are only published on business days. I put guard logic inside the function for when a given date has no data. Along with collecting, I picked up the sense of treating "a day with no data" as part of the normal flow.

Cleaning Then Loading to the DB

I cleaned the raw collection with pandas and stored it into MySQL via the to_db() module function made earlier. Separating collection code from DB I/O code into a module meant I could reuse it in other projects with a single from dbio import to_db.

Takeaway

Key point

The biggest lesson was "don't erase your failures." Keeping the error cells kept me from repeating the same mistake. The notebook itself also explained why the code looks the way it does. It's the same reason I keep this training log.