-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetKeyStats.py
170 lines (137 loc) · 4.62 KB
/
getKeyStats.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import sys, math
import urllib2
import re
from BeautifulSoup import BeautifulSoup
# Download the Key Statistics given a ticker symbol
# Return Key Statistics and list of Keys
def getKeyStats(ticker, DEBUG):
# Download Key Stats from http://finance.yahoo.com/q/ks?s=MA
# Open URL
# myURL='http://ichart.finance.yahoo.com/table.csv?'+\
# 's=%s&d=10&e=20&f=2010&g=d&a=9&b=20&c=2010'%t +\
# '&ignore=.csv'
#getKeyStatsNew( ticker, DEBUG) ;
#myURL='http://finance.yahoo.com/q/ks?s=%s'%ticker
#myURL='http://www.nasdaq.com/symbol/'%ticker
myURL='http://www.nasdaq.com/symbol/' + ticker
#print "In getKeyStats "
if (DEBUG ):
print myURL
c=urllib2.urlopen(myURL)
soup=BeautifulSoup(c.read())
if DEBUG:
print soup
#print "***** soup ends ***";
keyCount=0
key=""
value=""
keys={}
keyStats={}
keyFlag=True;
ValueFlag=False;
for data in soup('div'):
# Find the div with the class below
if ('class' in dict(data.attrs) and data['class']=='row overview-results relativeP'):
if DEBUG:
print "*** My My Found div ***"
print data
for tableCells in data('div'):
if ('class' in dict(tableCells.attrs) and tableCells['class']=='table-cell'):
#print "*** cell ***"
# print tableCells.contents
# print tableCells.getText();
if ( keyFlag ):
key=tableCells.getText();
keys[keyCount]=key
keyCount=keyCount + 1
keyFlag=False;
if DEBUG:
print "*** Key is ***"
print key
print len(key)
else:
value=tableCells.getText();
keyStats[key]=value
keyFlag=True;
if DEBUG:
print "*** value = ***"
print value
continue;
#key=""
#value=""
#keys={}
#keyStats={}
for td in soup('td'):
# Prints the heading
if ('class' in dict(td.attrs) and td['class']=='yfnc_tablehead1'):
key=td.text
keys[keyCount]=key
keyCount=keyCount + 1
if DEBUG:
print "*** Key is ***"
print key
continue
# Prints the Value
if ('class' in dict(td.attrs) and td['class']=='yfnc_tabledata1'):
value=td.text
if DEBUG:
print "*** value = ***"
print value
keyStats[key]=value
#print "keyStats[key] is " + keyStats[key]
continue
# Look for Title
allDivs=soup.findAll("title");
for div in allDivs:
value = div.getText();
key="title";
keys[keyCount]=key;
keyCount=keyCount + 1
keyStats[key]=value
#print "Title added"
#Printing keystats
if DEBUG:
for k in keyStats:
print keyStats[k]
print keyCount
return keyStats, keyCount
#def getValueFromKey( keyStats, key ):
# return keyStats[key]
def getValueFromKey( keyStats, key ):
returnValue="0.0";
#Check if key exists - Sep 2018
if ( key in keyStats ):
returnValue=keyStats[key];
#print returnValue;
# Strip out the %
returnValue=returnValue.replace('%','')
# Strip out the Commas
returnValue=returnValue.replace(',','')
returnValue=returnValue.replace(' ','')
returnValue=returnValue.replace('$','')
return returnValue
#Checks for None and returns Float value
def convertToFloat( dataToConvert ):
returnValue=dataToConvert;
if (( returnValue is None )or ( returnValue=="NA" ) or ( returnValue=="N/A" ) ):
returnValue=0.0;
else:
returnValue=float( returnValue );
return returnValue
#Sample data is here:
#MA Stock Quote - Mastercard Incorporated Common Stock Price - Nasdaq
#{u"Today's High / Low": u'$ 218.69 / $ 214.37', u'Forward P/E (1y)': u'33.39'
#, u'Dividend Payment Date': u'8/9/2018', u'1 Year Target': u'225'
#, 'title': u'MA Stock Quote - Mastercard Incorporated Common Stock Price - Nasdaq', u'Previous Close': u'$ 214.03'
#, u'Market Cap': u'240,871,804,202', u'90 Day Avg. Daily Volume': u'3,132,124', u'Beta': u'1.28', u'P/E Ratio': u'48.7'
#, u'Earnings Per Share (EPS)': u'$ 4.46', u'Share Volume': u'3,863,896', u'Current Yield': u'0.47 %', u'Ex Dividend Date': u'7/6/2018'
#, u'Annualized Dividend': u'$ 1', u'52 Week High / Low': u'$ 217.35 / $ 137.75'}
#Set DEBUG=True and run this directly in python. It should be set to False for app engine
DEBUG=False;
ticker="MA"
keyStats , keyCount =getKeyStats(ticker,DEBUG)
eps= getValueFromKey (keyStats, 'Earnings Per Share (EPS)' );
if ( DEBUG ):
print "EPS is " + eps;
print keyStats['title']
print keyStats, keyCount;