Python Program to dislay company share value from Yahoo! finance.

Standard

This one was given by kushal few days back to get acquainted with the urllib.
The objective was to take in a company symbol and print its current share value from Yahoo! finance.
This was my piece of code:

#!/usr/bin/env python
import urllib
a=raw_input('Enter  SYMBOL       : ')
url='http://finance.yahoo.com/q?s='+a+'&ql=1/'
con=urllib.urlopen(url)
if con.geturl().find('lookup')!=-1:
    print 'INVALID SYMBOL!!! Program will exit.'
    exit(0)
s=con.read()
search='tabledata1">'
i=s.find(search)
l=len(search)
i+=1
x=0
val=''
while(s[i].isdigit() or s[i]=='.' or s[i]==','):
    val=val+s[i]
    i+=1
a=s.find('title')+6
sum=''
while(s[a]!='<'):
    sum=sum+s[a]
    a+=1
print sum
print 'Current share value :',val

These were some of the test outputs…

[souradeep@souradeep Documents]$ chmod +x symbol.py
[souradeep@souradeep Documents]$ ./symbol.py
Enter  SYMBOL       : DELL
DELL: Summary for Dell Inc.- Yahoo! Finance
Current share value : 12.27
[souradeep@souradeep Documents]$ ./symbol.py
Enter  SYMBOL       : RHT
RHT: Summary for Red Hat, Inc. Common Stock- Yahoo! Finance
Current share value : 51.16
[souradeep@souradeep Documents]$ ./symbol.py
Enter  SYMBOL       : ORCL
ORCL: Summary for Oracle Corporation- Yahoo! Finance
Current share value : 29.10
[souradeep@souradeep Documents]$ ./symbol.py
Enter  SYMBOL       : SDE
INVALID SYMBOL!!! Program will exit.

I really enjoyed the program…my first one with the urllib.

Advertisements

One thought on “Python Program to dislay company share value from Yahoo! finance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s