Skip to content

Instantly share code, notes, and snippets.

@ryansmccoy
Last active January 29, 2019 01:13
Show Gist options
  • Save ryansmccoy/021caf443963850e01a339ba3848b248 to your computer and use it in GitHub Desktop.
Save ryansmccoy/021caf443963850e01a339ba3848b248 to your computer and use it in GitHub Desktop.
import pandas as pd
from datetime import datetime
df_calendar = pd.read_html(r'https://finance.yahoo.com/calendar/earnings/')
df_all_est = []
for ticker in df_calendar[0]['Symbol'].tolist():
try:
print(f'Downloading Estimates for {ticker}')
df_est = pd.read_html(r'https://finance.yahoo.com/quote/{ticker}/analysis?p={ticker}')
eps_est, rev_est = df_est[0], df_est[1]
rev_est = rev_est.melt(id_vars=['Revenue Estimate'],var_name='Period')
rev_est['Ticker'] = ticker
rev_est['Item'] = "REVENUES"
rev_est.rename(index=str, columns={"Revenue Estimate": "Statistic"}, inplace=True)
rev_est['DateTime'] = str(datetime.utcnow())
df_all_est.append(rev_est)
eps_est = eps_est.melt(id_vars=['Earnings Estimate'],var_name='Period')
eps_est['Ticker'] = ticker
eps_est['Item'] = "EPS"
eps_est.rename(index=str, columns={"Earnings Estimate": "Statistic"}, inplace=True)
eps_est['DateTime'] = str(datetime.utcnow())
df_all_est.append(eps_est)
print(eps_est.append(rev_est).to_string())
except Exception as e:
print(f"Error Downloading {ticker} \n{e}\n")
df_all_merged = pd.concat(df_all_est)
df_all_merged.reset_index(drop=True, inplace=True)
df_all_merged = df_all_merged.T.reindex(['DateTime', 'Ticker', 'Item', 'Statistic', 'Period', 'value']).T
df_all_merged.to_csv(r'{}_estimates.csv'.format(datetime.today().strftime("%Y%M%d")))
"""
DateTime Ticker Item Statistic Period value
0 2019-01-29 00:53:27.649059 MMM REVENUES No. of Analysts Current Qtr. (Dec 2018) 11
1 2019-01-29 00:53:27.649059 MMM REVENUES Avg. Estimate Current Qtr. (Dec 2018) 7.87B
2 2019-01-29 00:53:27.649059 MMM REVENUES Low Estimate Current Qtr. (Dec 2018) 7.75B
3 2019-01-29 00:53:27.649059 MMM REVENUES High Estimate Current Qtr. (Dec 2018) 8.03B
4 2019-01-29 00:53:27.649059 MMM REVENUES Year Ago Sales Current Qtr. (Dec 2018) 7.99B
5 2019-01-29 00:53:27.649059 MMM REVENUES Sales Growth (year/est) Current Qtr. (Dec 2018) -1.50%
6 2019-01-29 00:53:27.649059 MMM REVENUES No. of Analysts Next Qtr. (Mar 2019) 7
7 2019-01-29 00:53:27.649059 MMM REVENUES Avg. Estimate Next Qtr. (Mar 2019) 8.2B
8 2019-01-29 00:53:27.649059 MMM REVENUES Low Estimate Next Qtr. (Mar 2019) 8.1B
9 2019-01-29 00:53:27.649059 MMM REVENUES High Estimate Next Qtr. (Mar 2019) 8.41B
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment