Skip to content

Instantly share code, notes, and snippets.

@CapnKernel
Last active December 17, 2015 15:59
Show Gist options
  • Save CapnKernel/5635182 to your computer and use it in GitHub Desktop.
Save CapnKernel/5635182 to your computer and use it in GitHub Desktop.
USD to AUD fetching code
#! /usr/bin/env python
import re
import requests
from lxml import html
import json
import sys
oanda_url = 'http://www.oanda.com/currency/converter/'
oanda_params = {
'base_currency_0': 'USD',
'quote_currency': 'AUD',
}
# Get a session, for cookies etc
s = requests.session()
r = s.get(oanda_url, params=oanda_params)
r.raise_for_status()
tree = html.document_fromstring(str(r.text))
t = tree.xpath('/html/body/script[last()]')
assert len(t) == 1
# print len(t)
# print t
data_matcher = re.compile("(?s).*'data': ({.*}),\s*'currencies':")
data_matcher_result = data_matcher.search(t[0].text)
data = data_matcher_result.group(1).replace("'", '"')
# print "data=", data
j = json.loads(data)
# print "j=", j
cd = j['chart_data']
# print "cd=", cd
# print "len(cd)=", len(cd)
aud_usd_result = cd[len(cd)-1][3]
print "aud_usd_result=", aud_usd_result
usdaud = float(aud_usd_result)
print "-- usdaud=", usdaud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment