def parse_html(filename, charset=None, log=None):
+ parser = HTML5Parser()
fp = open(filename)
- html_tree = HTML5Parser().parse(fp, charset)
+ parser._parse(fp, encoding=charset, parseMeta=bool(charset))
fp.close()
+ html_tree = parser.tree.getDocument()
- if not html_tree.childNodes:
+ for node in html_tree.childNodes:
+ if (node.name == 'html') and (node.type != 3): # Skip DocType element
+ html = node
+ break
+ else:
+ html = None
+
+ if not html:
return None
- html = html_tree.childNodes[-1]
for node in html.childNodes:
if node.name == 'head':
head = node
else:
title = ''
+ if not charset:
+ charset = parser.tokenizer.stream.charEncoding[0]
+
if title and (charset or meta_charset):
title = title.encode(charset or meta_charset)