2 HTML Parser using html5.
4 Written by Broytman. Copyright (C) 2010 PhiloSoft Design
7 from html5lib import HTMLParser as HTML5Parser
8 from parse_html_util import HTMLParser
11 def parse_html(filename, charset=None, log=None):
13 html_tree = HTML5Parser().parse(fp, charset)
16 html = html_tree.childNodes[-1]
17 for node in html.childNodes:
18 if node.name == 'head':
30 for node in head.childNodes:
31 if node.name == 'meta' and \
32 ('http-equiv' in node.attributes) and \
33 (node.attributes['http-equiv'] == 'content-type'):
34 meta_content = node.attributes['content']
38 meta_content.lower().split('charset=')[1].split(';')[0]
44 for node in head.childNodes:
45 if node.name == 'title':
46 title = node.childNodes[0].value
49 if title and (charset or meta_charset):
50 title = title.encode(charset or meta_charset)
52 for node in head.childNodes:
53 if node.name == 'meta' and \
54 ('http-equiv' in node.attributes) and \
55 (node.attributes['http-equiv'] == 'refresh'):
56 refresh = node.attributes['content']
59 for node in head.childNodes:
60 if node.name == 'link' and \
61 ('rel' in node.attributes) and \
62 (node.attributes['rel'] in ('icon', 'shortcut icon')):
63 icon = node.attributes['href']
66 return HTMLParser(charset, meta_charset, title, refresh, icon)