]> git.phdru.name Git - bookmarks_db.git/blobdiff - Robots/parse_html_beautifulsoup.py
Check root.
[bookmarks_db.git] / Robots / parse_html_beautifulsoup.py
index f76f058f2578aabe430c2c403f919dad87c84b9d..c2a4032f042eb5c22930d9443da3fecb8cd9e5d7 100644 (file)
@@ -49,15 +49,27 @@ class BadDeclParser(BeautifulSoup):
          return j
 
 
-def parse_html(filename, charset=None, log=None):
+def _parse_html(filename, charset):
    infile = open(filename, 'r')
    try:
-      root = BadDeclParser(infile, fromEncoding=charset)
+      return BadDeclParser(infile, fromEncoding=charset)
    except TypeError:
       return None
    finally:
       infile.close()
 
+def parse_html(filename, charset=None, log=None):
+   root = _parse_html(filename, charset)
+   if root is None:
+      return None
+
+   _charset = root.originalEncoding
+   if _charset in ("ISO-8859-2", "windows-1252", "MacCyrillic"): # Replace default
+      _charset = DEFAULT_CHARSET
+      root = _parse_html(filename, _charset)
+      if root is None:
+         return None
+
    try:
       html = root.html
    except AttributeError:
@@ -76,10 +88,6 @@ def parse_html(filename, charset=None, log=None):
    if head is None:
       head = html # Some sites put TITLE in HTML without HEAD
 
-   _charset = root.originalEncoding
-   if _charset in ("ISO-8859-2", "windows-1252"): # Replace default
-      _charset = DEFAULT_CHARSET
-
    try:
       title = head.title.string.encode(_charset)
    except AttributeError: