Difficulty using htmllib

Joshua Goodlett joshua at goodish.org
Sat Jan 18 16:16:51 CET 2003

I've written a small web robot script to familiarize myself with both
the htmllib and urllib modules but have been getting an error message
when my HTMLParser subclass is fed.  I've included the relevant code,
as well as the error message, below:

----- snip -----

import htmllib
from formatter import NullFormatter

class URLExtractor(htmllib.HTMLParser):
    def __init__(self, outfile=None): 
        htmllib.HTMLParser.__init__(self, NullFormatter())
        self.url_list = []
    def anchor_bgn(self, href, name, type):
    def anchor_end(self): pass

    def reset(self)
        self.url_list = []

    def get_urls(self):
        return self.url_list

if __name__ == "__main__":

    parser = URLExtractor()
    hrefs = []

    urllist = ['www.4SEINC.com',
    for url in urllist:        
        sock = urllib.urlopen('http://' + url)
        data = sock.read()

----- snip -----

And now the error message...

----- snip -----

Traceback (most recent call last):
  File "parsertest.py", line 55, in ?
  File "C:\Python22\lib\sgmllib.py", line 95, in feed
  File "C:\Python22\lib\sgmllib.py", line 161, in goahead
    k = self.parse_declaration(i)
  File "C:\Python22\lib\markupbase.py", line 66, in parse_declaration
    decltype, j = self._scan_name(j, i)
  File "C:\Python22\lib\markupbase.py", line 313, in _scan_name
    self.error("expected name token")
  File "C:\Python22\lib\sgmllib.py", line 102, in error
    raise SGMLParseError(message)
sgmllib.SGMLParseError: expected name token
Press any key to continue . . .

----- snip -----

Any suggestions or advice as to the cause of the error would be
appreciated.  I'd suspected that the html source being fed to the
parser is malformed, but this may be wrong.

Joshua Goodlett

More information about the Python-list mailing list