Skip to content

Instantly share code, notes, and snippets.

View khaerulumam42's full-sized avatar
:octocat:

Khaerul Umam khaerulumam42

:octocat:
  • Jakarta, Indonesia
View GitHub Profile
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm_notebook as tqdm
import json
import pandas as pd
continent_codes = ['A', 'P', 'E', 'M', 'F']
country_names = []
country_codes = {}
for continent_code in continent_codes:
home = 'https://www.exchange-rates.org/currentRates/{}/USD'.format(continent_code)
home = requests.get(home)
home = home.text
soup_home_raw = BeautifulSoup(home, 'html.parser')
soup_home = soup_home_raw.find_all('td', attrs={'class':'text-left convert-to'})
for i, x in enumerate(soup_home):
z = x.text
z = z.split(' ')
currency_code = list(z[-1])[-3:]
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
results = {}
print("Progress scrapping")
for i, code in enumerate(tqdm(list(country_codes.values()))):
tmp_data = {}
webpage = 'https://www.exchange-rates.org/history/{}/USD/T'.format(code)
webpage = requests.get(webpage)
webpage = webpage.text
soup = BeautifulSoup(webpage, 'html.parser')
data = soup.find_all('div', attrs={'class':'table-responsive'})
with open('results.json', 'r') as f:
results = json.load(f)
with open('country_codes.json', 'r') as f:
country_codes = json.load(f)
import matplotlib
#matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()
for i, code in enumerate(list(results.keys())[0:4]):
pd_data = pd.DataFrame.from_dict(results[code])
pd_data['date'] = pd.to_datetime(pd_data['date'])
import json
import pandas as pd
import matplotlib
#matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()
with open('results.json', 'r') as f:
results = json.load(f)
with open('country_codes.json', 'r') as f:
country_codes = json.load(f)
def dictkeys_from_value(dictio, value):
idx_val = list(dictio.values()).index(value)
return list(dictio.keys())[idx_val]
diff = {}
daftar_negara = []
daftar_perubahan = []
for i, code in enumerate(list(results.keys())):
# proses ke dataframe supaya lebih mudah untuk ditampilkan ke data
pd_data = pd.DataFrame.from_dict(results[code])