
[Update 26/06/2021] I use beautiful soup to scrape morning star for EPS data. because of the way they have changed the way webpages are called it doesn't work anymore, however the data can be scraped with Selenium. I haven't had a chance to update the whole lecture, but the below code works for me, hopefully that helps you
"""
New version using webdriver
NNB! - Make sure chromedriver is up to date if its not working.
"""
from selenium import webdriver
from time import time
from time import sleep
import pandas as pd
from os import chdir
#-------------------Create Function to Collect Data---------------------
def GetData(ticker,findf):
#chdir("C:\\Users\\nr453") # Point to where the Chrome driver is for now
#already in saved in the investment directory
driver = webdriver.Chrome() # Instance of Chrome WebDriver is created.
driver.get(r"htt p : // financials.morningstar.com/ratios/r. html?t=" #link adjusted to post -fix to use
+ ticker +
r"®ion=usa&culture=en-US")
assert "Ratios" in driver.title # Confirm Ratios word in it.
sleep(2) # Wait for page to load
driver.find_element_by_class_name("large_button").click()
sleep(2) # Wait for download to start
assert "No results found." not in driver.page_source
driver.quit()
sleep(2) # Wait for Chrome window close (you can play with this time)
#--------------read CSV a dataframe----------------------
df = pd.read_csv(r'C:\\Users\\russe\Downloads\\'
+ ticker +
r' Key Ratios.csv', header = 2, index_col=False)
df.index = df['Unnamed: 0']
df = df.drop(['Unnamed: 0'], axis = 1)
df.index.names = ['Desc']
#-----------------Pull out the EPS data------------
dfeps = df.loc[['Earnings Per Share USD']].copy()
dfeps['name'] = ticker
dfeps.index = dfeps['name']
del dfeps['name']
#--------combine the two dataframes-------------------------
if j == 0: # If dataframe is empty
findf = dfeps # Save for the first time
else:
findf = findf.append(dfeps) # else just append
return findf
#-----------------------Import ticker data---------------------
chdir("C:\\Russell\\Investment\\Mkt_Val_Model")
splist = pd.read_csv("Inv201.02_List_All_US_Stocks_500m_INPY.csv",index_col=None) #, index_col=False
#--------------------Main dataframe to store data---------------
findf = pd.DataFrame() # Create empty data frame
FailedAttempt = [] # Store the names of stocks that have failed
t1 = time() # Start time
for j in range(0,len(splist)): #range(0,len(splist))
# Read in stock name (ticker)------------
ticker = splist['Symbol'][j].replace("-",".") # replace -'s with .'s
#-----Status Report---------------------
AveTime = round((time() - t1) / (j+1),2)
print ("Number %r of %r: %r Ave time: %r sec Time left: %r min/%r hr" %(j,len(splist),
ticker, AveTime, round(AveTime * (len(splist)-j)/60,2), round(AveTime * (len(splist)-j)/60/60,2)))
try:
findf = GetData(ticker, findf) # If not found, then it will skip to except to retry
except:
sleep(2)
FailedAttempt.append(ticker) # Save ticker
#----saving data to file ---
findf.to_csv("FinDat\\EpsSum01.csv")
FailedSeries = pd.Series(FailedAttempt) # convert from list
FailedSeries.to_csv("FinDat\\FailedTicker01.csv") # save failed ticker
SEE HOW VALUE INVESTING WAS USED TO MAKE A 27% PROFIT.
WHAT YOU GET IN THE "GET RICH SLOW" VIDEO COURSE:
[NOTE: Some of the python code in this course is no longer up to date, and no longer works,I'm working on patches. This course can still be useful to you to learn the method of this kind of value investing, and you can update the old code yourself, or wait for the patch]
Over 7.5 hours of step by step content teaching you value investing.
Step by step detail from where to look for investments, to how to analyse a company.
Learn how to rank thousands of US stocks in order of most undervalued to most overvalued.
Get a crash course coding in Python as you learn to scrape the necessary data from the internet.
Learn how to do a discounted cash flow (DCF) valuation of a stock in Excel.
Learn how to analyse a company for investment.
Learn when to buy & sell a stock.