-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockHunting-Selenium.py
151 lines (120 loc) · 5.58 KB
/
StockHunting-Selenium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from selenium import webdriver
'''READ-ME!
Temporary solution to Selenium driver processes lingering on
MacOS following any scripting error involving Selenium:
Go to 'Activity Monitor',
then 'CPU' or 'Memory' tab,
then search 'chromedriver'
and manually Quit each Process.'''
'''Stock Name + Ticker'''
ticker = 'dogef'
# ticker = input('Enter Stock Ticker: \n')
# Ratios tab
url = 'https://financials.morningstar.com/ratios/r.html?t=' + ticker + '®ion=usa&culture=en-US'
driver = webdriver.Chrome('/Users/admin/PycharmProjects/webscraping/chromedriver')
driver.get(url)
# Stock name + ticker
element_stock = driver.find_element_by_class_name('r_title').text
print(element_stock + '\n')
'''Interest Coverage Ratio
= Operating Income / Interest Expense
(Tells you how much operating income a company
generates relative to its interest expense.
Want a ratio of ≥ 6, and the higher the better.)'''
element_ICname = driver.find_element_by_id('i95').text
element_ICnum = driver.find_element_by_xpath('//*[@id="tab-profitability"]/table[2]/tbody/tr[16]/td[11]').text
if element_ICnum == '—':
print(element_ICname + ':', element_ICnum, '\n')
elif float(element_ICnum) > 6:
print(element_ICname + ':', element_ICnum, '(> 6. Great!)\n')
elif float(element_ICnum) == 6:
print(element_ICname + ':', element_ICnum, '(= 6. Not bad, but could be better.)\n')
else:
print(element_ICname + ':', element_ICnum, '(< 6. Bad.)\n')
# Free Cash Flow (found in 'Ratios' tab)
element_FCF = driver.find_element_by_xpath('//*[@id="financials"]/table/tbody/tr[26]/td[11]').text
# Earnings Per Share (found in 'Ratios' tab)
element_EPS = driver.find_element_by_xpath('//*[@id="financials"]/table/tbody/tr[12]/td[11]').text
# ROIC (found in 'Ratios' tab)
elements = driver.find_elements_by_id('i27')
for e in elements:
element_ROIC = e.find_element_by_xpath('//*[@id="tab-profitability"]/table[2]/tbody/tr[14]/td[11]').text
'''Debt-to-Assets Ratio
= Total Liabilities / Total Assets
(Want to see a Debt-to Assets ratio < 0.75, ideally. The lower the better.
Tells you how much the company OWES (debt) relative to
how much it OWNS (assets))'''
# Financials tab > Balance Sheet tab
url = 'http://financials.morningstar.com/balance-sheet/bs.html?t=' + ticker + '®ion=usa&culture=en-US'
driver.get(url)
# Total Assets
elements = driver.find_elements_by_id('data_tts1')
for e in elements:
TotalAssets = e.find_element_by_css_selector('#Y_5').text
print('Total Assets:', TotalAssets)
# Total Liabilities
elements = driver.find_elements_by_id('data_ttg5')
for e in elements:
TotalLiabilities = e.find_element_by_css_selector('#Y_5').text
print('Total Liabilities:', TotalLiabilities)
DAratio = round(float(TotalLiabilities.replace(',', ''))/float(TotalAssets.replace(',', '')), 2)
if DAratio < 0.75:
print('Debt-to-Assets Ratio:', DAratio, '(< 0.75. Great!)\n')
else:
print('Debt-to-Assets Ratio:', DAratio, '(> 0.75. Not so great.)\n')
'''Debt Payback Time
= Long-Term Debt / Free Cash Flow
(Tells you how long it would take for the company to pay
off all its debt.
A good rule of thumb is 3 years or less. You don't want to
invest in a company that has much debt that it'll take too
long to get out of debt.)'''
# Long-Term Debt (found in 'Balance Sheet' tab)
elements = driver.find_elements_by_id('data_i50')
for e in elements:
element_LTD = e.find_element_by_css_selector('#Y_5').text
print('Long-Term Debt:', element_LTD)
# print FCF
print('Free Cash Flow:', element_FCF)
DPT = round(float(element_LTD.replace(',', ''))/float(element_FCF.replace(',', '')), 2)
if DPT <= 3:
print('Debt Payback Time:', DPT, 'years (≤ 3 years. Great!)\n')
else:
print('Debt Payback Time:', DPT, 'years (> 3 years. Not so great.)\n')
'''Earnings Per Share (EPS)
= total net income of the company / the # of shares outstanding
(Want to look for strong, consistently positive & upward trending EPS numbers.
Looking at earnings on a per share basis makes it much easier to
read and compare with other companies.)'''
print('Earnings Per Share:', element_EPS + '\n' + '(want to see positive & upward trending)\n')
'''Return on Invested Capital (ROIC)
(Most important measure of profitability.
Shows how well the company is utilizing its resources to generate returns.
Look for companies w/ ROIC of ≥10%. The higher the better!)'''
if float(element_ROIC) >= 10:
print('Return on Invested Capital (ROIC):', element_ROIC + '%', '(≥ 10%. Great!)\n')
else:
print('Return on Invested Capital (ROIC):', element_ROIC + '%', '(< 10%. Not so great.)\n')
'''Price-to-Earnings (P/E) Ratio
= Stock Price / Earnings Per Share
(Quick & easy way to determine whether the stock is a bargain right now.
Compares the stock's current trading price to its most recently reported
earnings per share.
The lower the PE Ratio, the cheaper the stock relative
to the earnings its generates.
A good rule of thumb is to find stocks w/ PE ratios of 15 or less.
It helps to compare a stock's PE ratio w/ its historical average,
as well as w/ industry averages.
The lower the PE Ratio, the better the bargain!)'''
# Valuation tab
url = 'http://financials.morningstar.com/valuation/price-ratio.html?t=' + ticker + '®ion=usa&culture=en-US'
driver.get(url)
# PE Ratio
element_PER = driver.find_element_by_xpath('//*[@id="currentValuationTable"]/tbody/tr[2]/td[1]').text
if element_PER == '—':
print('Price-to-Earnings (P/E) Ratio:', element_PER + '\n')
elif float(element_PER) <= 15:
print('Price-to-Earnings (P/E) Ratio:', element_PER, '(≤ 15. Great!)\n')
else:
print('Price-to-Earnings (P/E) Ratio:', element_PER, '(> 15. Not so great.)\n')
driver.quit()