]> git.phdru.name Git - bookmarks_db.git/blobdiff - parse_html/bkmk_ph_html5.py
Fix(Py3): Fix HTML parsers
[bookmarks_db.git] / parse_html / bkmk_ph_html5.py
index 6400d02c20b924b0d77e65709c9136ab1647d41e..1fabd82166611fcf5c480463296e07390c906be1 100644 (file)
@@ -5,7 +5,7 @@ This file is a part of Bookmarks database and Internet robot.
 """
 
 __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']
@@ -17,14 +17,19 @@ from .bkmk_ph_util import HTMLParser
 
 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
@@ -58,7 +63,8 @@ def parse_html(html_text, charset=None, log=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:
@@ -70,8 +76,8 @@ def parse_html(html_text, charset=None, log=None):
         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 \