X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Robots%2Fparse_html_beautifulsoup.py;h=93a4d6fd9a2ba27ba0c1de2fdcdb3fe9971461c0;hb=896c8750d5b4e7a833156cc87a1f2f5847729904;hp=25719ca9d643ad86e2c2844c572b938d1bf6f792;hpb=5f637b5a5fe27098985975928632b9fea5ea3c62;p=bookmarks_db.git diff --git a/Robots/parse_html_beautifulsoup.py b/Robots/parse_html_beautifulsoup.py index 25719ca..93a4d6f 100644 --- a/Robots/parse_html_beautifulsoup.py +++ b/Robots/parse_html_beautifulsoup.py @@ -49,39 +49,51 @@ 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: - if log: log("TypeError") return None finally: infile.close() +def parse_html(filename, charset=None, log=None): + root = _parse_html(filename, charset) + + _charset = root.originalEncoding + if _charset in ("ISO-8859-2", "windows-1252", "MacCyrillic"): # Replace default + _charset = DEFAULT_CHARSET + root = _parse_html(filename, _charset) + try: - head = root.html.head + html = root.html except AttributeError: - if log: log("No HTML in root or no HEAD in HTML") - return None + if log: log("No HTML in root") + html = root - if head is None: - head = root.html # Some sites put TITLE in HTML without HEAD + if html is None: + html = root - _charset = root.originalEncoding - if _charset == "windows-1252": # Replace default - _charset = DEFAULT_CHARSET + try: + head = html.head + except AttributeError: + if log: log("No HEAD in HTML") + head = html + + if head is None: + head = html # Some sites put TITLE in HTML without HEAD try: title = head.title.string.encode(_charset) except AttributeError: title = '' # HEAD but no TITLE - if (not title) and (head is not root.html): + if (not title) and (head is not html): # Some sites put TITLE in HTML outside of HEAD try: - title = root.html.title.string.encode(_charset) + title = html.title.string.encode(_charset) except AttributeError: title = '' # no TITLE in HTML too