X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Robots%2Fparse_html_beautifulsoup.py;h=dc61a3305a3c0509562cfa9df56ccb88d550dc60;hb=dcad728f7b334f67f4f883add324de805b0b1e40;hp=632ee9f15cfaafec9a9f14ac8135354af655efac;hpb=3f74e8242f3fb1474ed258d97d791018f3bc1a34;p=bookmarks_db.git diff --git a/Robots/parse_html_beautifulsoup.py b/Robots/parse_html_beautifulsoup.py index 632ee9f..dc61a33 100644 --- a/Robots/parse_html_beautifulsoup.py +++ b/Robots/parse_html_beautifulsoup.py @@ -39,7 +39,7 @@ class BadDeclParser(BeautifulSoup): except SGMLParseError: # Could not parse the DOCTYPE declaration # Try to just skip the actual declaration - match = re.search(r']*?)>', self.rawdata[i:], re.MULTILINE) + match = re.search(r']*?)>', self.rawdata[i:], re.MULTILINE|re.IGNORECASE) if match: toHandle = self.rawdata[i:match.end()] else: @@ -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", "MacCyrillic"): # Replace default - _charset = DEFAULT_CHARSET - try: title = head.title.string.encode(_charset) except AttributeError: