1 """HTML Parser using Pythons' HTMLParser
3 This file is a part of Bookmarks database and Internet robot.
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 1997-2023 PhiloSoft Design"
9 __license__ = "GNU GPL"
11 __all__ = ['parse_html']
15 from HTMLParser import HTMLParseError
17 class HTMLParseError(Exception): pass
18 from m_lib.net.www.html import HTMLParser as _HTMLParser
21 class HTMLHeadDone(Exception): pass
24 class HTMLParser(_HTMLParser):
25 def __init__(self, charset=None):
26 _HTMLParser.__init__(self)
27 self.charset = charset
36 def do_meta(self, attrs):
40 for attrname, value in attrs:
43 if attrname == 'http-equiv':
44 http_equiv = value.lower()
45 elif attrname == 'content':
47 elif (attrname == 'charset') and (not self.charset):
48 self.charset = value.lower()
51 if (not self.charset) and (http_equiv == "content-type"):
53 # extract charset from
54 # "text/html; foo; charset=UTF-8, bar; baz;"
55 self.charset = content.lower().split('charset=')[1].\
56 split(';')[0].split(',')[0]
57 # Remember that the charset was retrieved from
58 # META tag, not from the Content-Type header
63 if http_equiv == "refresh":
64 self.refresh = content
66 def start_title(self, attrs):
70 if not self.title: # use only the first title
71 self.title = self.accumulator
73 def do_link(self, attrs):
77 for attrname, value in attrs:
80 if (attrname == 'rel') and (
81 value.lower() in ('icon', 'shortcut icon')
84 elif attrname == 'href':
91 def parse_html(html_text, charset=None, log=None):
94 parser = HTMLParser(charset)
97 parser.feed(html_text)
98 except (HTMLParseError, HTMLHeadDone):
103 except (HTMLParseError, HTMLHeadDone):
106 if (parser.title is None) and (parser.refresh is None) \
107 and (parser.icon is None):