Skip to content

Instantly share code, notes, and snippets.

@slmcmahon
Created February 24, 2020 21:56
Show Gist options
  • Save slmcmahon/48632aea533d56e5567fe3b65034108e to your computer and use it in GitHub Desktop.
Save slmcmahon/48632aea533d56e5567fe3b65034108e to your computer and use it in GitHub Desktop.
Python scrape for exchange rates against USD.
from bs4 import BeautifulSoup
import requests
import collections
currencies = []
Currency = collections.namedtuple('Currency', 'country usd')
sd = requests.get("https://www.x-rates.com/table/?from=USD&amount=1")
sp = BeautifulSoup(sd.content, 'html.parser')
table = sp.find('table', {"class": "tablesorter ratesTable"})
rows = table.findChildren('tr')
for row in rows:
cells = row.findChildren('td')
if len(cells) > 0:
country = cells[0].text
usd = cells[1].text
currencies.append(Currency(country=country, usd=usd))
for currency in currencies:
print("{0: <25} {1: >15}".format(currency.country, currency.usd))
print("-" * 41)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment