Showing posts with label opening web page by python. Show all posts
Showing posts with label opening web page by python. Show all posts

Wednesday, August 5, 2015

Finding Your Favorite Share Price by Using Python

For moving on to my next blog where I will make a GUI to search my favorite stock price and display it in a browser like environment, you need to grasp the idea of getting values from sites with python. So I have revisited urllib and write the program below fro your food for thought. 
 The htmllib and the formatter class is imported for very purpose of showing data in a mannered way.Lets go deep into the program  -


import urllib,htmllib,formatter,sys #importing the needed processes
page=urllib.urlopen("https://www.google.com/finance?q=NSE%3AMCX&ei=eIqoVcGIEsuYuATZpZ3gBw")

"""Go to the very location of the web the desired share price is displayed and updated in a regular basis. page is referenced to the web address"""
text=page.read().decode("utf8") #Read the page and decode it using utf8. Save it in a text variable
#print text #you can see the raw output from here new_fromat=formatter.AbstractFormatter(formatter.AbstractWriter())

#Convert the page in a new_format and write it by AbstractWriter() mode.
ptext=htmllib.HTMLParser(new_format)

#The HTMLParser will store data in new_format defined mode.
ptext.feed(text) #feed the text to the ptext to print it properly.
text=text.lstrip()
ptext.close()
where=text.find("ref_272295776418103_l") #This is the hard work mode as you need to find the exact position where the required value is or after which text the stock price is
print "--------------------------MCX---------------------------------------------"
print "The MCX Share Price ",text[where+23:where+31]

#Printing the Share Price of the particular share, The rest of the program is same old repetition.
 

print "--------------------------South Indian Bank------------------------- "

page2=urllib.urlopen("https://www.google.com/finance?q=NSE%3ASOUTHBANK&ei=hIqoVeCMG4OmuASin7SABw")
text2=page2.read().decode("utf8")
#print text2
text2=text2.lstrip().rstrip()
where2=text2.find("ref_679317_l")
print where2
#print "this is where _____-" 


print "South Indian Bank Share Price:",text2[where2+14:where2+19]
print "----------------------------------Axis Bank ----------------"
page3=urllib.urlopen("https://www.google.com/finance?q=NSE%3AAXISBANK&sq=axis%20bank&sp=1&ei=sga6VcGgCsyguAST-bP4CA")
text3=page3.read().decode("utf8")
text3=text3.lstrip()
#print text3
where3=text3.find("ref_3261730_l")
print "Axis Bank Price :"+text3[where3+15:where3+21]



The program will find the share price of my favorite three shares and display it. The formatter part of the program will be needed when we will create our own web browser.

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...