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 if not html_tree.childNodes:
19 html = html_tree.childNodes[-1]
20 for node in html.childNodes:
21 if node.name == 'head':
33 for node in head.childNodes:
34 if node.name == 'meta' and \
35 ('http-equiv' in node.attributes) and \
36 (node.attributes['http-equiv'] == 'content-type'):
37 meta_content = node.attributes['content']
41 meta_content.lower().split('charset=')[1].split(';')[0]
47 for node in head.childNodes:
48 if node.name == 'title':
50 title = node.childNodes[0].value
55 if title and (charset or meta_charset):
56 title = title.encode(charset or meta_charset)
58 for node in head.childNodes:
59 if node.name == 'meta' and \
60 ('http-equiv' in node.attributes) and \
61 (node.attributes['http-equiv'] == 'refresh'):
62 refresh = node.attributes['content']
65 for node in head.childNodes:
66 if node.name == 'link' and \
67 ('rel' in node.attributes) and \
68 (node.attributes['rel'] in ('icon', 'shortcut icon')):
69 icon = node.attributes['href']
72 return HTMLParser(charset, meta_charset, title, refresh, icon)