structure of SGMLParser sample from diveintopython
<p>I am just trying to adopt sample from chapter <a href="http://www.diveintopython.net/html_processing/index.html" rel="nofollow">http://www.diveintopython.net/html_processing/index.html</a>. Idea is to change urls inside in html file source. I came up with next code</p>

<pre><code>from BaseHTMLProcessor import BaseHTMLProcessor 

class ProxyParser(BaseHTMLProcessor):

    def __init__(self):
        BaseHTMLProcessor.__init__(self)
        self.urls = []

    def start_a(self, attrs):
        newattrs = []
        for s in attrs:
            if s[0] != 'href':
                newattrs.append((s[0], s[1]))
            else:
                ref = s[1]
                ref = self.amend_reference(ref)
                newattrs.append((s[0], ref))

     def amend_reference(self, ref):
         if ref.startswith('http://'):
               return 'http://localhost:8000/redirect?url='+ref
         elif ref.startswith('www'):
               return 'http://localhost:8000/redirect?url=http://'+ref
         else:
               return 'http://localhost:8000/redirect?url=' + self.baseurl +'/' + ref

     def base_url(self, url):
           self.baseurl = url
</code></pre>

<p>how to buildup html text back and how to apply ammende url to correspond  tags from htmlsource</p>
