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):
12 parser = HTML5Parser()
14 parser._parse(fp, encoding=charset, parseMeta=bool(charset))
16 html_tree = parser.tree.getDocument()
18 for node in html_tree.childNodes:
19 if (node.name == 'html') and (node.type != 3): # Skip DocType element
28 for node in html.childNodes:
29 if node.name == 'head':
41 for node in head.childNodes:
42 if node.name == 'meta' and \
43 ('http-equiv' in node.attributes) and \
44 (node.attributes['http-equiv'] == 'content-type'):
45 meta_content = node.attributes['content']
49 meta_content.lower().split('charset=')[1].split(';')[0]
55 for node in head.childNodes:
56 if node.name == 'title':
58 title = node.childNodes[0].value
64 charset = parser.tokenizer.stream.charEncoding[0]
66 if title and (charset or meta_charset):
67 title = title.encode(charset or meta_charset)
69 for node in head.childNodes:
70 if node.name == 'meta' and \
71 ('http-equiv' in node.attributes) and \
72 (node.attributes['http-equiv'] == 'refresh'):
73 refresh = node.attributes['content']
76 for node in head.childNodes:
77 if node.name == 'link' and \
78 ('rel' in node.attributes) and \
79 (node.attributes['rel'] in ('icon', 'shortcut icon')):
80 icon = node.attributes['href']
83 return HTMLParser(charset, meta_charset, title, refresh, icon)