[Tutor] finding title tag (fwd)

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 17 Oct 2001 11:20:38 -0700 (PDT)


Dear Samir,

I can't answer your question at the moment, so I'll forward this to the
rest of the tutor list.  It's usually a good idea to send messages to the
whole list, so that many people have an opportunity to put their two cents
in.

---------- Forwarded message ----------
Date: Wed, 17 Oct 2001 18:26:23 +1000 (EST)
From: Samir Patel <sampatel@cs.rmit.edu.au>
To: Danny Yoo <dyoo@hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] finding title tag (fwd)

hi,
well actually if u will look at my code i have implemented resursion in my
program but using methods only....i am not comfortable with methods in a
class and then applying recursion......

i have done recursion and i understand the concept..but bit confuse with
python.....i have done java,C and stuff....

now as far as depth is concerned it will be given by the user and so it
can be more then 3...that's the reason i have implemented rescursion ....

i read that there's something called associative array ...can we implement
that for link as key and title as the value...but again don't know how to
actually implement that in a class.....
in my case i will have to implement class as i have to use method
start_tag() and end_tag()....

i did tried to write a program using this methods and also implemented
associative array...but can't apply recursion on them...


#!/usr/local/bin/python

import sys
import urllib
from formatter import NullFormatter
from htmllib import HTMLParser

class myparser(HTMLParser):

   def __init__(self):

       HTMLParser.__init__(self,NullFormatter())
       self.linktext = {}
       self.url = None

   def start_a(self,attr):

       for key,val in attr:


           if key == 'title':
	        self.url = val
       HTMLParser.save_bgn(self)

   def end_a(self):

       self.linktext[self.url] = HTMLParser.save_end(self)

try:
      f = urllib.urlopen(sys.argv[1])
except IndexError:
      f = sys.stdin

p = myparser()
p.feed(f.read())

for link in p.linktext.keys():

    print '-> ' , link , p.linktext[link]

#sorry to bother u ...

and thanx for your help....

above code is different from the code i have actually implemented....but i
want to implement above code if possible but with recursion and
associative array.....

thanx a lot

samir