"""
__author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 2010-2014 PhiloSoft Design"
+__copyright__ = "Copyright (C) 2010-2023 PhiloSoft Design"
__license__ = "GNU GPL"
__all__ = ['parse_html']
def parse_html(html_text, charset=None, log=None):
parser = HTML5Parser()
- html_tree = parser.parse(html_text, encoding=charset, parseMeta=bool(charset))
-
- for node in html_tree.childNodes:
- if (node.name == 'html') and (node.type != 3): # Skip DocType element
- html = node
- break
+ if isinstance(html_text, bytes):
+ html_tree = parser.parse(
+ html_text, encoding=charset, parseMeta=bool(charset))
else:
- html = None
+ html_tree = parser.parse(html_text)
+
+ html = None
+ if hasattr(html_tree, 'childNodes'):
+ for node in html_tree.childNodes:
+ # Skip DocType element
+ if (node.name == 'html') and (node.type != 3):
+ html = node
+ break
if not html:
return None
if meta_content:
try:
meta_charset = \
- meta_content.lower().split('charset=')[1].split(';')[0]
+ meta_content.lower().split('charset=')[1].\
+ split(';')[0]
except IndexError:
meta_charset = False
else:
if not charset:
charset = parser.tokenizer.stream.charEncoding[0]
- if title and (charset or meta_charset):
- title = title.encode(charset or meta_charset)
+ #if title and (charset or meta_charset):
+ # title = title.encode(charset or meta_charset)
for node in head.childNodes:
if node.name == 'meta' and \