Skip to content

Instantly share code, notes, and snippets.

@ericjang
Last active July 30, 2017 01:00
Show Gist options
  • Save ericjang/1bb70d267dae9b7d3485 to your computer and use it in GitHub Desktop.
Save ericjang/1bb70d267dae9b7d3485 to your computer and use it in GitHub Desktop.
Python script that returns a pandas dataframe containing global Consumer Price Indices
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
@rsvp
Copy link

rsvp commented Jul 30, 2017

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment