-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patheps.py
More file actions
56 lines (46 loc) · 1.21 KB
/
eps.py
File metadata and controls
56 lines (46 loc) · 1.21 KB
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
#!/usr/bin/python
#eps.py
# Chapter 17 Stock Market
# Author: William C. Gunnells
# Rapid Python Programming
# libs
import json
import urllib
from pprint import pprint
from BeautifulSoup import *
base_url = 'https://query.yahooapis.com/v1/public/yql?'
def querytool(sql):
query = {
'q': sql,
'format': 'json',
'env': 'store://datatables.org/alltableswithkeys'
}
return query
def getdata(base_url,qtool):
url = base_url + urllib.urlencode(qtool)
response = urllib.urlopen(url)
data = response.read().decode('utf-8')
quote = json.loads(data)
return quote
def getEPS(symbol):
ar=[]
num=0;token=0
url = "http://finance.yahoo.com/q?s=" + symbol +"&q1=1"
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
soup = BeautifulSoup(htmltext)
table=soup.findAll('td')
for row in table:
ar.append(row.text)
num+=1
if 'P/E' in row.text:
token=num
return ar[token]
if __name__=="__main__":
symbol="AAPL"
sql='select * from yahoo.finance.quote where symbol in ("%s")' % symbol
Q=querytool(sql)
stock=getdata(base_url,Q) # returns json or nested dict
data=stock['query']['results']['quote'] # relevant stock data dictionary
data['P/E']=getEPS(symbol)
print data