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
icon = None
if head:
+ for node in head.childNodes:
+ if node.name == 'title':
+ if node.childNodes:
+ title = node.childNodes[0].value
+ break
+ else:
+ title = ''
+
+ if title is None:
+ return None
+
for node in head.childNodes:
if node.name == 'meta' and \
('http-equiv' in node.attributes) and \
else:
break
- for node in head.childNodes:
- if node.name == 'title':
- if node.childNodes:
- title = node.childNodes[0].value
- break
- else:
- title = ''
+ if not charset:
+ charset = parser.tokenizer.stream.charEncoding[0]
- if title and (charset or meta_charset):
+ if charset or meta_charset:
title = title.encode(charset or meta_charset)
for node in head.childNodes:
icon = node.attributes['href']
break
+ else:
+ for node in html.childNodes:
+ if node.name == 'title':
+ if node.childNodes:
+ title = node.childNodes[0].value
+ break
+ else:
+ title = ''
+
+ if title is None:
+ return None
+
return HTMLParser(charset, meta_charset, title, refresh, icon)