Showing posts with label python urllib. Show all posts
Showing posts with label python urllib. 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.

Tuesday, August 4, 2015

Introduction to urllib

Before I move on to the little browser project which will open a web page and show the value my favorite hare price and also can be used to see basic html view of a website, I need to introduce you to a the urllib class of python.

So,open your IDLE and write import urllib, then press enter. You then type help(urllib) and take a quick glance on the reading material there. Then, let;s start. Lets open my site

import urllib
page=urllib.urlopen('http://pythonbeginner.in')
print page.read()

only these three steps are enough to open a webpage and read the html file to your idle console. look at another program -


import urllib2
import repage = urllib2.urlopen('http://www.pythonbeginner.in').read()
matches = re.findall('python', page);
if len(matches) == 0:
   print ('Not about python')
else:
   print ('My site is about python')


Look at the program above. I have used another module urllib2 and also the re (Regular Expression) module. urlopen again opens the file and reads it. Then we search for 'python' string in the whole html file and if we found it then print 'My site is about python' otherwise 'Not about python'. Don't bother about regular expression as I will cover it in later section.

The urllib and urlopen are the classs we will need in the next example but you practice all the method and class defined in dir(urllib) or urllib2. Sometimes html tags are awful and if I need to remove the tagging then we have to do the things which the next program will demonstrate -

import urllib,htmllib,sys,formatter
page=urllib.urlopen("https://www.pythonbeginner.in")
data=page.read()

page.close()
new_format=formatter.AbstractFormatter(formatter.DumbWriter(sys.stdout))
ptext=htmllib.HTMLParser(new_format)
ptext.feed(data)
ptext.close()


The last program is your food for thought as a lot of things are not defined yet and I will define and describe the procedure in due time. Please subscribe for next blog entry.

 

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...