Last active
July 30, 2017 01:00
-
-
Save ericjang/1bb70d267dae9b7d3485 to your computer and use it in GitHub Desktop.
Python script that returns a pandas dataframe containing global Consumer Price Indices
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
import requests | |
from bs4 import BeautifulSoup | |
import pandas as pd | |
import re | |
import sys | |
def currentCPI: | |
url = 'http://www.tradingeconomics.com/country-list/consumer-price-index-(cpi)' | |
r = requests.get(url) | |
soup = BeautifulSoup(r.text) | |
datatable_header = soup.find('thead', attrs={'class':'datatable-header'}) | |
headers = datatable_header.findAll('th') | |
H = [h.text.strip() for h in headers] | |
H[0] = u'Country' | |
del H[-1] | |
datatable_rows = soup.findAll('tr', attrs={'class':'datatable-row'}) | |
data = [] | |
for row in datatable_rows: | |
td = row.findAll('td') | |
rowvals = {} | |
for i in range(len(H)): | |
rowvals[H[i]] = td[i].text.strip() | |
data.append(rowvals) | |
df = pd.DataFrame(data, columns=H) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How stable over time is the structure of the data page?
Might use for https://github.com/rsvp/fecon235
Please advise me at https://gitter.im/rsvp/fecon235 -- thanks!