Created
August 19, 2014 00:06
-
-
Save ericjang/90b93bc07b2bee623d8a to your computer and use it in GitHub Desktop.
Python script that retrieves a pandas dataframe containing fundamental stock market valuation ratios
This file contains hidden or 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 globalValues: | |
url = 'http://www.starcapital.de/research/stockmarketvaluation?SortBy=Shiller_PE' | |
r = requests.get(url) | |
if r.status_code != 200: | |
print('Could not retrieve funds page. Code %d' % r.status_code) | |
sys.exit() | |
soup = BeautifulSoup(r.text) | |
img_header_str = soup.find(attrs={'class':'img_header'}).text | |
m = re.search('(?<=per)(.*)', img_header_str) | |
date_str = m.groups()[0].strip() | |
tmp = soup.findAll(attrs={'class':'years', 'scope':'col'}) | |
headers = [] | |
for t in tmp: | |
headers.append(t.text) | |
nCols = len(headers) | |
data = [] | |
tmp = soup.findAll(attrs={'scope':'row'}) | |
for t in tmp: # for each country | |
country = t.text | |
p = t.parent # traverse up the node | |
matches = p.findAll(attrs={'class':re.compile("^(mz|mzm)$")}) | |
vals = {} | |
for i in range(nCols): | |
val = matches[i].text | |
vals[headers[i]] = val # these are strings, not numbers | |
data.append(vals) | |
df = pd.DataFrame(data, columns=headers) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment