Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Scraping Forex Data

Scraping Forex Data

I was looking for ways to scrape forex data after switched to a subscription model with rate-limiting, making their API useless for lots of queries.

1. Use an alternative API

One alternative API which is still free is

It's basically a drop-in replacement for and can be used to get all forex rates in json format at

To set your base currency just use an addition to the URL as follows

See the website documentation for more detail.

2. Write your own one-liner to create the json

I discovered the various forex APIs are all just calling info from the European Central Bank's published data, which is updated once a day.

However the format is in XML, which isn't very useful for my scripts which are setup to use JSON.

So we need to modify the XML into JSON format. It turns out to be simple enough with a one-liner in bash.

This assumes you have installed yq, a companion to jq which also includes xq which is for XML files. If you like searching json with jq you'll love searching YAML/XML with yq and xq.

If you have jq installed, and python-pip, then installing is as simple as:

pip install yq

Next, the following one-liner bash script will scrape ECB forex rates (Euro-base) and output into a JSON file you can query with jq in a second process:

curl -s |xq '.[]|.["Cube"]["Cube"]["Cube"] | .[] |map(values)' |sed -e "s/,/:/" |tr '\n' ' ' |tr '[' '{' |tr ']' '}' |sed -e "s/ } { /, /g" |jq -c -r '.'

which produces output as follows


You can pipe it to a file, and perform additional operations on that file, or even narrow it down to specific currencies as follows:

curl -s |xq '.[]|.["Cube"]["Cube"]["Cube"] | .[] |map(values)' |sed -e "s/,/:/" |tr '\n' ' ' |tr '[' '{' |tr ']' '}' |sed -e "s/ } { /, /g" |jq -c -r '.ZAR'

It shouldn't be hard to append date info either.

i.e. basically what is doing and charging you for, or is doing

3. Scrape Standard Bank's feed

It was brought to my attention that a local bank publishes forex info at the following URL

But this isn't in a very useful format for scripts.

I've previously built a python script to scrape html tables into a csv file, and it works just as well here. Please see the link for more info on prerequisites to get it working.

# 2017-12-05  Bretton  initial creation
# this file copied from
# usage:
# curl URL | /full/path/to/
# and html table will be output as csv file

from bs4 import BeautifulSoup
import sys
import re
import csv

def cell_text(cell):
    return " ".join(cell.stripped_strings)

soup = BeautifulSoup(, "html.parser")
output = csv.writer(sys.stdout)

for table in soup.find_all('table'):
    for row in table.find_all('tr'):
        col = map(cell_text, row.find_all(re.compile('t[dh]')))

Now we have a URL to scrape:

and a script to turn the tables into CSV:


But unfortunately the output isn't very pretty, so we'll have to do some more bash operations to get the output we want.

There are multiple ways of tackling this, and picking which column you want to apply, which I'll leave to you.

However the following work for me:

curl -s "" | ./ |grep USD| awk -F, '{print $5}'




curl -s "" | ./ |grep USD| awk -F, '{print $5, $7}' |tr ' ' '\n'

produces buy/sell values, which can be used to calculate a spread or in/out flow of your calculations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.