Created
February 24, 2020 21:56
-
-
Save slmcmahon/48632aea533d56e5567fe3b65034108e to your computer and use it in GitHub Desktop.
Python scrape for exchange rates against USD.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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