1 """HTML Parser using Pythons' HTMLParser
3 This file is a part of Bookmarks database and Internet robot.
6 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 1997-2011 PhiloSoft Design"
11 __license__ = "GNU GPL"
13 __all__ = ['parse_html']
16 from HTMLParser import HTMLParseError
17 from m_lib.net.www.html import HTMLParser as _HTMLParser
20 class HTMLHeadDone(Exception): pass
23 class HTMLParser(_HTMLParser):
24 def __init__(self, charset=None):
25 _HTMLParser.__init__(self)
26 self.charset = charset
35 def do_meta(self, attrs):
39 for attrname, value in attrs:
42 if attrname == 'http-equiv':
43 http_equiv = value.lower()
44 elif attrname == 'content':
47 if (not self.charset) and (http_equiv == "content-type"):
49 # extract charset from "text/html; foo; charset=UTF-8, bar; baz;"
50 self.charset = content.lower().split('charset=')[1].split(';')[0].split(',')[0]
51 self.meta_charset = 1 # Remember that the charset was retrieved from
52 # META tag, not from the Content-Type header
56 if http_equiv == "refresh":
57 self.refresh = content
59 def start_title(self, attrs):
63 if not self.title: # use only the first title
64 self.title = self.accumulator
66 def do_link(self, attrs):
70 for attrname, value in attrs:
73 if (attrname == 'rel') and (value.lower() in ('icon', 'shortcut icon')):
75 elif attrname == 'href':
82 def parse_html(filename, charset=None, log=None):
83 infile = open(filename, 'r')
84 parser = HTMLParser(charset)
89 except (HTMLParseError, HTMLHeadDone):
96 except (HTMLParseError, HTMLHeadDone):
99 if parser.title is None: